SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Member crumblepie's Avatar
    Join Date
    Nov 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question No entries in database even though form submitted successfully? What could be wrong?

    Here is the code:

    PHP Code:
    $values 'VALUES ("'.$user_name.'", "'.$user_name.'", "'.$user_name.'", "'.$user_pass.'", "'.$user_email.'", "'.user_ip.'")';
            
    $insert mysql_query("INSERT INTO member(user_name, bidder_name, seller_name, user_pass, user_email, user_ip) " .$values);
            if(
    $insert)
            {
                    
    ///Yippie it's done, now ask that guy to login
                    
    header('location: login.php');
            }
        }
    mysql_close($conn);
    ?> 
    In the member database, the columns are user_name, bidder_name, seller_name, user_pass, user_ip, user_email in that order. The registration form just asks for username, password and e-mail address. I am using
    PHP version 5.3.10
    MySQL version 5.0.95-community

    What could be wrong? How come when someone submits the form and it says it's successful, but there are no entries in the database? Could it be because I upgraded my version of MYSQL?

  2. #2
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $values = 'VALUES ("'.$user_name.'", "'.$user_name.'", "'.$user_name.'", "'.$user_pass.'", "'.$user_email.'", "'.$user_ip.'")';


    Might be because you are missing $ sign before user_ip field.

  3. #3
    SitePoint Member crumblepie's Avatar
    Join Date
    Nov 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good point mrana, thanks!

    I fixed that but after submitting the form, I'm still seeing a blank row in mysql

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,323
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    a blank row? does it at least have a value for the primary key?

    echo your sql statement before executing it, to ensure that the $ variables have successfully been substituted into the sql code
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    // Get values from form
    $name=$_POST['name'];
    $lastname=$_POST['lastname'];
    $email=$_POST['email'];

    // Insert data into mysql
    $sql="INSERT INTO $tbl_name(name, lastname, email)VALUES('$name', '$lastname', '$email')";
    $result=mysql_query($sql);

    // if successfully insert data into database, displays message "Successful".
    if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='form.php'>Back to main page</a>";
    }

    else {
    echo "ERROR";
    }

    // close connection
    mysql_close();
    ?>


    Try this, this way you will know if the error is in sql statement or not.

  6. #6
    SitePoint Member crumblepie's Avatar
    Join Date
    Nov 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    a blank row? does it at least have a value for the primary key?

    echo your sql statement before executing it, to ensure that the $ variables have successfully been substituted into the sql code
    This table has no primary key (although most of the other tables do). I'm not sure how to echo, I'm a total newbie when it comes to PHP.

    Quote Originally Posted by mrana View Post
    // Get values from form
    $name=$_POST['name'];
    $lastname=$_POST['lastname'];
    $email=$_POST['email'];

    // Insert data into mysql
    $sql="INSERT INTO $tbl_name(name, lastname, email)VALUES('$name', '$lastname', '$email')";
    $result=mysql_query($sql);

    // if successfully insert data into database, displays message "Successful".
    if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='form.php'>Back to main page</a>";
    }

    else {
    echo "ERROR";
    }

    // close connection
    mysql_close();
    ?>


    Try this, this way you will know if the error is in sql statement or not.
    I tried it and it says "ERROR" after submitting the form. What is an sql statement? I appreciate your help!

  7. #7
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is something wrong with the insert statement. Can you give more details about the all the database fields you have and your insert statement. Then I would be glad to help you.

  8. #8
    SitePoint Member crumblepie's Avatar
    Join Date
    Nov 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi mrana,

    You are the best!

    Here is the entire process.php file:

    PHP Code:
    <?php
    session_start
    ();
    ///What i need to do here is to check the length of password and username
    $LengthName strlen($user_name);
    $LengthPass strlen($user_pass);
    if((
    $LengthName 10)&&($LengthPass 10))
        {
            echo 
    'Maximum length is 10 for password and username';
            exit();
        }
    ///Second step is to find out whether user input the password correctly
    $a $_POST['user_pass'];
    $b $_POST['user_pass2'];
    if(
    $a != $b)
        {
            echo 
    'Your password does not match... Go back and retype your password';
            exit();
        }
    ///Check for email address
    if(empty($_POST['user_email']))
        {
            echo 
    'Enter your email address';
            exit();
        }
    ///Check for image verification to prevent registerBot
    $number $_POST['image'];
    if(
    md5($number) != $_SESSION['image_random_value'])
        {
            echo 
    'Verification does not match.. Go back and refresh your browser and then retype your verification';
            exit();
        }
    else
        {
            
    ///Open connection for registration process
            
    include 'inc/config.php';
            include 
    'inc/conn.php';
            
    ///Check whether username exist
            
    $sql "SELECT * FROM member WHERE user_name = '".$_POST['user_name']. "'";
            
    $CheckUser mysql_query($sql$conn);
            
    $NumRow mysql_num_rows($CheckUser);
            if(
    $NumRow == 1)
                {
                    echo 
    'Username '.$user_name' exist.. Please choose different username';
                    
    mysql_close($conn);
                    exit();
                }
            
    ///Ok... no record found. I can process the registration
            
    $values 'VALUES ("'.$user_name.'", "'.$user_name.'", "'.$user_name.'", "'.$user_pass.'", "'.$user_email.'", "'.user_ip.'")';
            
    $insert mysql_query("INSERT INTO member(user_name, bidder_name, seller_name, user_pass, user_email, user_ip) " .$values);
            if(
    $insert)
            {
                    
    ///Yippie it's done, now ask that guy to login
                    
    header('location: login.php');
            }
        }
    mysql_close($conn);
    ?>
    and this is what the member table looks like.

    I could give you my cPanel info if that would help.

  9. #9
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,323
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by crumblepie View Post
    I'm not sure how to echo, I'm a total newbie when it comes to PHP.
    i don't do php at all, so you're ahead of me

    change this --
    Code:
    $values = 'VALUES ("'.$user_name.'", "'.$user_name.'", "'.$user_name.'", "'.$user_pass.'", "'.$user_email.'", "'.user_ip.'")';
             $insert = mysql_query("INSERT INTO member(user_name, bidder_name, seller_name, user_pass, user_email, user_ip) " .$values);
    to this --
    Code:
    $values = 'VALUES ("'.$user_name.'", "'.$user_name.'", "'.$user_name.'", "'.$user_pass.'", "'.$user_email.'", "'.user_ip.'")';
    $insertsql = "INSERT INTO member(user_name, bidder_name, seller_name, user_pass, user_email, user_ip) " .$values ;
    echo $insertsql
    $insert = mysql_query($insertsql);
    Quote Originally Posted by crumblepie View Post
    What is an sql statement?
    the sql statement starts with INSERT and includes the VALULES clause
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  10. #10
    SitePoint Member crumblepie's Avatar
    Join Date
    Nov 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had to insert a ; after the echo part to make it work, and I got this:

    INSERT INTO member(user_name, bidder_name, seller_name, user_pass, user_email, user_ip) VALUES ("", "", "", "", "", "")
    Warning: Cannot modify header information - headers already sent by (output started at /home/domain/public_html/process.php:50) in /home/domain/public_html/process.php on line 55


    Btw, line 55 is:
    header('location: login.php');

  11. #11
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,323
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by crumblepie View Post
    INSERT INTO member(user_name, bidder_name, seller_name, user_pass, user_email, user_ip) VALUES ("", "", "", "", "", "")
    this explains why your database contains a blank row

    it looks like your variables are undefined
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  12. #12
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    r937 is correct I don't see the variables defined in the code.

  13. #13
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Woohoo, Rudy does PHP debugging! Nice one!

  14. #14
    SitePoint Member crumblepie's Avatar
    Join Date
    Nov 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is my register.php:

    PHP Code:
    <?php
    ///start session
    session_start();
    // is the one accessing this page logged in or not?
    if (!isset($_SESSION['member']) || $_SESSION['member'] !== true
    {
        
    // not logged in
        
    $un 'Gen';
    }
    if(
    $member == $_POST['user_name'])
    {
        
    $un $_POST['user_name'];
    }
    include 
    'inc/config.php';
    include 
    'inc/conn.php';
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title><?php echo $site; echo ' Registration'?></title>
    <meta name="keywords" content="<?php echo $keywords?>">
    <meta name="description" content="<?php echo $description?>">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <link href="css/index.css" rel="stylesheet" type="text/css">
    </head>

    <body>
    <div align="center">
    <?php include 'inc/header.php'?>
    <table width="950" border="0" class="outer" cellpadding="0" cellspacing="0">
      <tr> 
        <td width="164" valign="top"> 
          <?php include 'inc/nav.php'?>
        </td>
        <td valign="top">
        <table width="100%" cellpadding="0" class="innerbox">
            <tr>
                <td><p>By being our member, you will be able to place bid for our 
                  domain listing and has the advantage of listing your domain in our 
                  database. Register now! It's free!</p>
                <p>If you had already a member, you can login <a href="login.php">here</a></p></td>
            </tr>
        </table>
        
        <form method="post" action="process.php">
            <table width="100%" border="0" class="innerbox">
              <tr> 
                <td width="34%"  height="20" class="tit">Register a new account</td>
                <td width="66%"  class="tit">Fill in all the blank</td>
            </tr>
            <tr></tr>
            <td>Username: max10</td>
            <td><input type="text" name="user_name" maxlength="10"></td>
            </tr>
            <tr> 
              <td>Password: max10</td>
              <td><input type="text" name="user_pass" maxlength="10"></td>
            </tr>
            <tr> 
              <td>Retype Password:</td>
              <td><input type="text" name="user_pass2" maxlength="10"></td>
            </tr>
            <tr> 
            <tr> 
              <td>Email:</td>
              <td><input type="text" name="user_email" maxlength="100"></td>
            </tr>
            <tr> 
              <td>Verification:</td>
              <td><input type="text" name="image"> <img src="inc/image.php"></td>
            </tr>
            <tr> 
              <td>&nbsp;</td>
              <td><div align="right"> 
                    <input name="user_ip" type="hidden" value="<?php echo $HTTP_SERVER_VARS['REMOTE_ADDR']; ?>">
                  <input name="submit" type="submit" value="Submit">
                </div></td>
            </tr>
          </table></form>
          
          </td>
      </tr>
    </table>
    <?php include 'inc/footer.php'?>
    Aren't the variables defined there already? I mean by submitting the form?

  15. #15
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See if this example will help you ahead - http://www.phpeasystep.com/mysql/5.html

  16. #16
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
            $values 'VALUES ("'.$user_name.'", "'.$user_name.'", 
    try alterting to

    PHP Code:
            $values 'VALUES ("'.$_POST['user_name'].'", "'.$_POST['user_name'].'", 
    ... and then stop, and now go and read about protecting yourself from sql injection attacks in PHP.

  17. #17
    SitePoint Member crumblepie's Avatar
    Join Date
    Nov 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cups, that worked!

    You guys are awesome! mrana, your tutorial link helped me understand the codes better, and r937, you guided me in the right direction. Cups, you are a lifesaver! I shall read the info on that link. I've got the majority of the Sitepoint books but was too lazy to finish reading them (they are awesome, btw!) and figured I might as well use this script I found. It was driving me crazy for days! Thank you so much!!! There is no rep button here, is there?

  18. #18
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    We only ever had a RTFM button, but someone removed it.


Bookmarks

Posting Permissions

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