Results 1 to 15 of 15

Thread: [RESOLVED] Really new at this

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Resolved [RESOLVED] Really new at this

    Just started out with PHP.Made a nice little page in DW04,using form to send data to my database created in MYSQL.

    So here is my code:

    form:
    Code:
    <form name="form1" method="post" action="insert.php">
            ime: <input type="text" name="Ime" />
            prezime: <input type="text" name="Prezime" />
            email: <input type="text" name="email" />
            komentar: <textarea name="komentar"></textarea>
            <input type="submit" name="POSALJI" value="POSALJI">
    </form>
    and my insert.php

    Code:
    <?php
    $con = mysql_connect("localhost","myname","mypass");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("korisnik", $con);
    
    $sql="INSERT INTO podaci (ime, prezime,email,komentar)
    VALUES
    ('$_POST[ime]','$_POST[prezime]','$_POST[email]','$_POST[komentar])";
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo $sql;
    
    mysql_close($con)
    ?>
    The thing is,nothing happens!No data is inserted,no error.Echo displays nothing.Guess I'm missing something here,so if anyone can help...thank you!

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Really new at this

    you have a parse error. your call to mysql_close() is not terminated (ends with a semicolon); but the call to this function is also un-needed. PHP will automatically close the connection to MySQL, so you can remove that line completely.

    you're also missing a single quote in your SQL query (just after $_POST[komentar]), which would make your query not run. but, before inserting any of that information into the database, you should make sure that none of it could break your script. you should never trust input from your users. you can use the function mysql_real_escape_string() to escape all of the necessary characters for your query, in case your user uses single quotes or other such characters.
    PHP Code:
    $ime mysql_real_escape_string($_POST['ime']);
    //repeat for prezime, email, and komentar

    $sql="INSERT INTO podaci (ime, prezime,email,komentar) VALUES ('$ime','$prezime','$email','$komentar')"
    you can also temporarily turn on error reporting by PHP (if you have it off in your php configuration) by using the error_reporting() function at the top of your script.
    PHP Code:
    error_reporting(E_ALL); 
    Last edited by kows; Apr 12th, 2009 at 03:09 PM.

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Really new at this

    Error reporting should always be turned on to the maximum level during development. To turn it on for every script you need to modify your php.ini file.

    What OS are you running PHP on; or are you using hosting space?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: Really new at this

    I'm running WinXP,PHP 5.2.9 and Apache HTTP Server 2.2

    When I type http://localhost/insert.php into my browser i get this error:

    Fatal error: Call to undefined function mysql_connect() in C:\Documents and Settings\admin\My Documents\Test\insert.php on line 3

    I read somewhere that php.ini needs to be edited,but dont know how to solve that!


    ---edit---

    Some more info(edited php.ini)

    my php.ini file is in C:\WINDOWS
    I uncommented the php_mysql.dll
    Copied libmysql.dll to C:\Windows\System
    set doc_root to: "C:\Documents and Settings\admin\My Documents\Test"

    Dont know what else to do
    Last edited by BlackRiver; Apr 12th, 2009 at 05:38 PM.

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Really new at this

    in php.ini, make sure the extension_dir directive is correctly pointing at the directory you have your extensions in. by default, I think it's set to "./", which wouldn't load any of your extensions unless they were in the root php installation directory. I usually use a full path when installing on windows -- something like c:/php5/ext -- but you could also probably just change it to "./ext". alternatively, you could just move php_mysql.dll to the root PHP installation directory and everything should be fine.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: Really new at this

    It is pointing at my ext directory and php_mysql.dll is in it!Still,I get the same error!

  7. #7
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Really new at this

    you've restarted Apache since making all of these changes, right?

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: Really new at this

    Ooh,I have done everything as described in this tutorial,but when I get to the last part it starts loading and then:

    "Apache HTTP Server has encountered a problem...." and it crashes without showing any data.I really dont know what else to do.Any my deadline is Thursday

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: Really new at this

    Hmmm,I got the select statement working,but my insert into doesnt work!

    This is my insert.php:

    Code:
    <?php
    error_reporting(E_ALL);
    $con = mysql_connect("localhost","root","napoleon");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("korisnik", $con);
    $ime = mysql_real_escape_string($_POST['ime']);
    $prezime=mysql_real_escape_string($_POST['prezime'];
    $email=mysql_real_escape_string($_POST['email'];
    $komentar=mysql_real_escape_string($_POST['komentar'];
    
    $sql="INSERT INTO podaci (ime,prezime,email,komentar) VALUES ('$ime','$prezime','$email','$komentar')"; 
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo $sql;
    ?>
    and my html code is same as the above.And still nothing happens

  10. #10
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Really new at this

    Make sure the libmysql.dll file is copied into either the PHP directory or directory within the PATH environment such as \Windows\System32
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: Really new at this

    That file is in PHP Directory and in \Windows\System32
    Strange really

  12. #12
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Really new at this

    Do you still get a startup error from Apache?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  13. #13
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Really new at this

    no, he said his script was working now :x

    you're missing the end parenthesis on every call you made to mysql_real_escape_string(). just add that, and your script will run. I've highlighted the lines missing it:

    Code:
    $ime = mysql_real_escape_string($_POST['ime']);
    $prezime=mysql_real_escape_string($_POST['prezime'];
    $email=mysql_real_escape_string($_POST['email'];
    $komentar=mysql_real_escape_string($_POST['komentar'];
    you have error reporting on, so when you run a script like this and get an error, it makes a world of difference if you would just post it as well :)

  14. #14
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Really new at this

    I appeared to miss post #9

    Another option to change is display_errors = On in php.ini. This is turned off by default in the latest versions of PHP.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: Really new at this

    Woke up this morning,looked over my code again and noticed the missing parenthesis.Fixed that!Entered data into textboxes,submited it and it works!Checked back here and smiled at kows post Anyways,thank you guys very much! Resolved!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width