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

Here is the 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?

$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.

Good point mrana, thanks!

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

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

// 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.

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.

I tried it and it says “ERROR” after submitting the form. What is an sql statement? I appreciate your help!

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.

Hi mrana,

You are the best!

Here is the entire process.php file:

<?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.

i don’t do php at all, so you’re ahead of me :slight_smile:

change this –

$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 –


$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 ;
[COLOR="#FF0000"]echo $insertsql[/COLOR]
$insert = mysql_query($insertsql); 

the sql statement starts with INSERT and includes the VALULES clause

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’);

this explains why your database contains a blank row

it looks like your variables are undefined

r937 is correct I don’t see the variables defined in the code.

Woohoo, Rudy does PHP debugging! Nice one!

This is my register.php:

<?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?

See if this example will help you ahead - http://www.phpeasystep.com/mysql/5.html

        $values = 'VALUES ("'.$user_name.'", "'.$user_name.'",

try alterting to

        $values = 'VALUES ("'.$_POST['user_name'].'", "'.$_POST['user_name'].'",

… and then stop, and now go and read about [google]protecting yourself from sql injection attacks in PHP[/google].

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?

We only ever had a RTFM button, but someone removed it. :slight_smile: