Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts

    error attaching form to database! help needed :(

    hi all,

    firstly i have been watching some tutorial videoswhich were a great help, so i wanted to try my new found knowledge but failed badly

    what im trying to do is create a "sign up" form with 7 fields First Name, Last name, Age, Username, Password etc etc,

    but i want it so when the user submits the form it adds the info to the database i setup, called login_test with 1 table called user_details which has the same 7 fields in it with the username being unique.

    but i have absolutly no idea how to make the users input, add to the database,

    so far i have:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>

    <?php

    $my_connection = mysql_connect('localhost','root', '');

    if (!$my_connection) {
    die('Could Not Connect: ' . mysql_error());
    }

    echo 'Connected Successfully to MySQL Server' . '<br><br>';

    mysql_select_db('login_test');

    if (!my_database){
    die('could not find database: ' . mysql_error());
    }

    //form processing code - using 'super globals' - Killerphp.com

    $first_name = $_REQUEST['name_first'];
    $last_name = $_REQUEST['name_last'];
    $age = $_REQUEST['age'];
    $address = $_REQUEST['address'];
    $postcode = $_REQUEST['postcode'];
    $username = $_REQUEST['username'];
    $password = $_REQUEST['password'];

    mysql_query("INSERT INTo user_details (First Name ,Last Name,Age, Address , Postcode, Username , Password) VALUES ('$first_name' , '$last_name' , '$age', '$Address', '$postcode' , '$username' , '$password')");

    $result = mysql_query("SELECT * FROM user_details");

    echo $result;


    mysql_close($my_connection);

    ?>
    </body>
    </html>

    but i keep getting: Connected Successfully to MySQL Server

    Resource id #3


    instead of it listing all the data

    any ideas???
    cheers
    Luke

  • #2
    New Coder
    Join Date
    Aug 2008
    Posts
    17
    Thanks
    4
    Thanked 1 Time in 1 Post
    Quote Originally Posted by LJackson View Post
    hi all,

    firstly i have been watching some tutorial videoswhich were a great help, so i wanted to try my new found knowledge but failed badly

    what im trying to do is create a "sign up" form with 7 fields First Name, Last name, Age, Username, Password etc etc,

    but i want it so when the user submits the form it adds the info to the database i setup, called login_test with 1 table called user_details which has the same 7 fields in it with the username being unique.

    but i have absolutly no idea how to make the users input, add to the database,

    so far i have:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <?php
    
    $my_connection = mysql_connect('localhost','root', '');
    
    if (!$my_connection) {
    die('Could Not Connect: ' . mysql_error());
    }
    
    echo 'Connected Successfully to MySQL Server' . '<br><br>';
    
    mysql_select_db('login_test');
    
    if (!my_database){
    die('could not find database: ' . mysql_error());
    }
    
    //form processing code - using 'super globals' - Killerphp.com
    
    $first_name = $_REQUEST['name_first'];
    $last_name = $_REQUEST['name_last'];
    $age = $_REQUEST['age'];
    $address = $_REQUEST['address'];
    $postcode = $_REQUEST['postcode'];
    $username = $_REQUEST['username'];
    $password = $_REQUEST['password'];
    
    mysql_query("INSERT INTo user_details (First Name ,Last Name,Age, Address , Postcode, Username , Password) VALUES ('$first_name' , '$last_name' , '$age', '$Address', '$postcode' , '$username' , '$password')");
    
    $result = mysql_query("SELECT * FROM user_details");
    
    echo $result;
    
    
    mysql_close($my_connection);
    
    ?>
    </body>
    </html>
    but i keep getting: Connected Successfully to MySQL Server

    Resource id #3


    instead of it listing all the data

    any ideas???
    cheers
    Luke
    Try instead of echo $result; use print_r($result);

    I didn't test it but it should work.
    Last edited by zackwiny; 08-10-2008 at 10:04 PM.

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    unfortunatly i get the same error and when i check my sql database its empty so im doing something wrong somewhere

    just not sure where lol

    Luke

  • #4
    New Coder
    Join Date
    Aug 2008
    Posts
    17
    Thanks
    4
    Thanked 1 Time in 1 Post
    Well the problem is that i think $result is an array and can't be echoed,
    or it might be that $result is like a placeholder for the query but isn't echoing the result of the query,
    can't really help you much more,

    Sry

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok cheers mate

  • #6
    Regular Coder
    Join Date
    Jan 2007
    Posts
    142
    Thanks
    9
    Thanked 1 Time in 1 Post
    You should clean up the insert code some. Fix the comma placement.

    I'd also name my fields in my database to remove the spaces, like First_Name.

    Make sure your age field is set as an integer with at least 2 in the length.

    You can't just echo the result like that. Try this for a test. It will only work AFTER you get stuff into the database.

    Code:
    $result = mysql_query("SELECT * FROM user_details");
    
    while ($row = mysql_fetch_array($result)) 
    {
    	echo '' . $row['First Name'] . '<br />';
    	echo '' . $row['Last Name'] . '<br />';
    	echo '' . $row['Age'] . '<br />';
    	echo '' . $row['Address'] . '<br />';
    	echo '' . $row['Postcode'] . '<br />';
    	echo '' . $row['Username'] . '<br />';
    	echo '' . $row['Password'] . '';
    }
    I would make sure you fix those table names to remove the spaces.

  • Users who have thanked unrelenting for this post:

    LJackson (08-11-2008)

  • #7
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,851
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    @LJackson, you need to fetch the data from the mysql query result(as given in the above post), see the manual for further examples and similar function, http://php.net/mysql_query
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    thanks guys, now have records saving to database and echoing on screen, however 2 fields dont save or echo, address which ive set as a text string, and password which is set as a password in the form but they dont save, i ve checked the code and everything seems ok

    <PHP>
    <?php

    $my_connection = mysql_connect('localhost','root', '');

    if (!$my_connection) {
    die('Could Not Connect: ' . mysql_error());
    }

    echo 'Connected Successfully to MySQL Server' . '<br><br>';

    mysql_select_db('login_test');

    if (!my_database){
    die('could not find database: ' . mysql_error());
    }

    //form processing code - using 'super globals' - Killerphp.com

    $first_name = $_REQUEST['name_first'];
    $last_name = $_REQUEST['name_last'];
    $age = $_REQUEST['age'];
    $address = $_REQUEST['address'];
    $postcode = $_REQUEST['postcode'];
    $username = $_REQUEST['username'];
    $password = $_REQUEST['password'];

    mysql_query("INSERT INTO user_details (first_name, last_name, age, address , postcode, username , password) VALUES ('$first_name' , '$last_name' , '$age', '$address', '$postcode' , '$username' , '$password')");

    $result = mysql_query("SELECT * FROM user_details");

    while ($row = mysql_fetch_array($result))
    {
    echo '' . $row['first_name'] . '<br />';
    echo '' . $row['last_name'] . '<br />';
    echo '' . $row['age'] . '<br />';
    echo '' . $row['address'] . '<br />';
    echo '' . $row['postcode'] . '<br />';
    echo '' . $row['username'] . '<br />';
    echo '' . $row['password'] . '';
    }
    mysql_close($my_connection);

    ?>
    </PHP>

    p.s what did you mean by saying clean up you commas?

    cheers


  •  

    Posting Permissions

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