Need help with code!

Discussion in 'PHP' started by jonny2210, Nov 25, 2006.

  1. #1
    Im making a simple mysql php login, here is the code for inserting the code into a mysql database.



    <?php

    $host = localhost;
    $username = "deleted";
    $password = "deleted";
    $usern = $_POST[name];
    $passw = $_POST[pass];
    $database = "deleted";

    mysql_connect($host,$username,$password);
    @mysql_select_db($database) or die( "Can't retrieve database.");
    $query="INSERT INTO user (username, password) values
    ('$usern', '$passw')";
    mysql_query($query);
    if (!mysql_query($query))
    {
    die('Error: ' . mysql_error());
    }
    echo "1 user added";
    mysql_close($host,$username,$password)
    ?>


    The mysql table name is 'user' and has 'username' and 'password' in the fields.
    when i run the script it only adds the password to mysql database and leaves the username empty, how do I fix that.

    thanks, Jonathan
     
    jonny2210, Nov 25, 2006 IP
  2. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you're inserting data into your table using:
    $query="INSERT INTO user (username, password) values
    ('$usern', '$passw')";
    Code (markup):
    how sure are you that $usern contains a value? before that line, can you do a simple print to confirm that there is a value in the variable? i suspect it's empty:

    print "username to be inserted: " . $usern . "<br><br>";
    $query="INSERT INTO user (username, password) values
    ('$usern', '$passw')";
    Code (markup):
     
    daboss, Nov 25, 2006 IP
  3. jonny2210

    jonny2210 Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks sooo much. found out that on the html form i made the username variable 'user' not 'name'. Thanks.
     
    jonny2210, Nov 25, 2006 IP