Form info into db

Discussion in 'MySQL' started by W3C, Nov 28, 2006.

  1. W3C

    W3C New Member

    Joined:
    Nov 24, 2006
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Okay so here is the problem. I made a form. When I click submit the info doesn't go into the database. Here is the code:

    PHP:
    <?php
    ob_start
    ();
    // allows you to use cookies
    include("config.php");
    //gets the config page
    if ($_POST[register]) {
    // the above line checks to see if the html form has been submitted
    $username $_POST[username];
    $password $_POST[pass];
    $cpassword $_POST[cpass];
    $email $_POST[emai1];
    //the above lines set variables with the user submitted information
    if($username==NULL|$password==NULL|$cpassword==NULL|$email==NULL) {
    //checks to make sure no fields were left blank
    echo "A field was left blank.";
    }else{
    //none were left blank!  We continue...
    if($password != $cpassword) {
    // the passwords are not the same!  
    echo "Passwords do not match";
    }else{
    // the passwords are the same!  we continue...
    $password md5($password);
    // encrypts the password
    $checkname mysql_query("SELECT username FROM users WHERE username='$username'");
    $checknamemysql_num_rows($checkname);
    $checkemail mysql_query("SELECT email FROM users WHERE email='$email'");
    $checkemail mysql_num_rows($checkemail);
    if (
    $checkemail>0|$checkname>0) {
    // oops...someone has already registered with that username or email!
    echo "The username or email is already in use";
    }else{
    // noone is using that email or username!  We continue...
    $username htmlspecialchars($username);
    $password htmlspecialchars($password);
    $email htmlspecialchars($email);
    // the above lines make it so that there is no html in the user submitted information.
    //Everything seems good, lets insert.
    $query mysql_query("INSERT INTO users (username, password, email) VALUES('$username','$password','$email')");
    // inserts the information into the database.
    echo "You have successfully registered!<br />";
    echo 
    "Click <a href=\"editprofile.php\">here</a> to edit your profile.";
    $sql "$username$password$email";
    echo 
    $sql;
    }
    }
    }
    }
    else
    {
    // the form has not been submitted...so now we display it.
    echo ("
    <center>
    <form method=\"POST\">
    Username: <input type=\"text\" size=\"15\" maxlength=\"25\" name=\"username\"><br />
    Password: <input type=\"password\" size=\"15\" maxlength=\"25\" name=\"pass\"><br />
    Confirm Password: <input type=\"password\" size=\"15\" maxlength=\"25\" name=\"cpass\"><br />
    Email: <input type=\"text\" size=\"15\" maxlength=\"25\" name=\"emai1\"><br />
    <input name=\"register\" type=\"submit\" value=\"Register\">
    </form>
    </center>
    "
    );
    }
    ?>
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    48
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Did you get any errors?? Please post the specific errors.
    I also see that you did not connect to the DB before the query.
     
  3. W3C

    W3C New Member

    Joined:
    Nov 24, 2006
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    I do not get any erros at all.
    And connecting to the DB, I think that is what
    PHP:
    include('config')
    is for in the 4th line.
     
  4. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    48
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Try replacing $query = mysql_query("INSERT INTO users (username, password, email) VALUES('$username','$password','$email')"); with
    PHP:
    $query mysql_query("INSERT INTO users (username, password, email) VALUES('$username','$password','$email')") or die(mysql_error());
    And check whether it prints out anything or not.
     
  5. W3C

    W3C New Member

    Joined:
    Nov 24, 2006
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Ok now I get the error "Field 'id' doesn't have a default value" and thats all it says It doesn't print out the info though.
     
  6. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    48
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    OK. Modify the table, so that id field is auto_increment, or insert an value for id, in the query.
     
  7. W3C

    W3C New Member

    Joined:
    Nov 24, 2006
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Okay. I did that and it says:
    "You have successfully registered. Click here to edit your profile. Then it displays wahtever you typed in."
    I click the link to edit profile. It then shows you must login. When I log in it says "Incorrect Username or password." This is the code for the login.

    PHP:
    <?php
    oB_start
    ();
    // allows you to use cookies.
    include("config.php");
    if (!
    $logged[username])
    {
    if (!
    $_REQUEST[login])
    {
    echo(
    "
    <center><form method=\"REQUEST\">
    <table>
    <tr>
    <td align=\"right\">
    Username: <input type=\"text\" size=\"15\" maxlength=\"25\" name=\"username\">
    </td>
    </tr>
    <tr>
    <td align=\"right\">
    Password: <input type=\"password\" size=\"15\" maxlength=\"25\" name=\"password\">
    </td></tr><tr>
    <td align=\"center\">
    <input type=\"submit\" name=\"login\" value=\"Login\">
    </td></tr><tr>
    <td align=\"center\">
    <a href=\"register.php\">Register Here</a>
    </td></tr></table></form></center>"
    );
    }
    if (
    $_REQUEST[login]) {
    // the form has been submitted.  We continue...
    $username=$_REQUEST['username'];
    $password=$_REQUEST['password'];
    // the above lines set variables with the submitted information.  
    $info1 mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
    $data1 = @mysql_fetch_array($info1);
    if(
    $data1[username] != $username)
    $info2 mysql_query("SELECT * FROM users WHERE password = '$password'") or die(mysql_error());
    $data2 = @mysql_fetch_array($info2);
    if(
    $data2[password] != $password) {
    // the password was not the user's password!
    echo "Incorrect username or password!";
    }else{
    // the password was right!
    $query mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
    $user mysql_fetch_array($query);
    // gets the user's information
    setcookie("id"$user[id],time()+(60*60*24*5), "/""");
    setcookie("pass"$user[password],time()+(60*60*24*5), "/""");
    // the above lines set 2 cookies. 1 with the user's id and another with his/her password.  
    echo ("<meta http-equiv=\"Refresh\" content=\"0; URL=http://localhost/>Thank You! You will be redirected");
    // modify the above line...add in your site url instead of yoursite.com
    }
    }
    }
    else
    {
    // we now display the user controls.
    echo ("<center>Welcome <b>$logged[username]</b><br /></center>
    - <a href=\"editprofile.php\">Edit Profile</a><br />
    - <a href=\"members.php\">Member List</a><br />
    - <a href=\"logout.php\">Logout</a>"
    );
    }
    ?>
     
  8. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    48
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    <form method=\"REQUEST\"> there is no such method as REQUEST, there are only 2 methods, GET and POST.
    I also see that you have used a super global variable $_REQUEST, which is also non-existent unless explicitally created.
     
  9. W3C

    W3C New Member

    Joined:
    Nov 24, 2006
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Well I got it to work. But I thought REQUEST was a method. It says something about the super global $_REQUEST on this one site that it was. I changed quite a few things in the code. It may look the same but some parts a different, and I have added a few extra lines.

    PHP:
    <?php
    oB_start
    ();
    // allows you to use cookies.
    include("config.php");
    if (!
    $logged[username])
    {
    if (!
    $_REQUEST[login])
    {
    echo(
    "
    <center><form method=\"REQUEST\">
    <table>
    <tr>
    <td align=\"right\">
    Username: <input type=\"text\" size=\"15\" maxlength=\"25\" name=\"username\">
    </td>
    </tr>
    <tr>
    <td align=\"right\">
    Password: <input type=\"password\" size=\"15\" maxlength=\"25\" name=\"password\">
    </td></tr><tr>
    <td align=\"center\">
    <input type=\"submit\" name=\"login\" value=\"Login\">
    </td></tr><tr>
    <td align=\"center\">
    <a href=\"register.php\">Register Here</a>
    </td></tr></table></form></center>"
    );
    }
    if (
    $_REQUEST[login]) {
    // the form has been submitted.  We continue...
    $username=$_REQUEST['username'];
    $password=$_REQUEST['password'];
    // the above lines set variables with the submitted information.  
    $info1 mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
    $data1 = @mysql_fetch_array($info1);
    if(
    $data1[username] != $username){
    echo 
    "Incorrect username!";
    }else{
    $info2 mysql_query("SELECT * FROM users WHERE password = '$password'") or die(mysql_error());
    $data2 = @mysql_fetch_array($info2);
    if(
    $data2[password] != $password) {
    // the password was not the user's password!
    echo "Incorrect password!";
    }else{
    // the password was right!
    $query mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
    $user = @mysql_fetch_array($query);
    // gets the user's information
    setcookie("id"$user[id],time()+(60*60*24*5), "/""");
    setcookie("pass"$user[password],time()+(60*60*24*5), "/""");
    // the above lines set 2 cookies. 1 with the user's id and another with his/her password.  
    echo "<meta http-equiv=\"Refresh\" content=\"0; URL=http://localhost/KS%20Prac%20Sites/KRAZY_SPACE/login.php>Thank You! You will be redirected";
    // modify the above line...add in your site url instead of yoursite.com
    }
    }
    }
    }
    else
    {
    // we now display the user controls.
    echo ("<center>Welcome <b>$logged[username]</b><br /></center>
    - <a href=\"editprofile.php\">Edit Profile</a><br />
    - <a href=\"members.php\">Member List</a><br />
    - <a href=\"logout.php\">Logout</a>"
    );
    }
    ?>
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice