Page 1 of 1

Problem inserting values into a database

Posted: Wed Nov 15, 2006 10:55 am
by evilchris2003
Hello

The below script is causing me some issues all im trying to do is register a user with the database
when it is run the page posts and returns the same HTML form as far as i can tell (well and educated guess really) is that the script runs the 1st set of IF statements (to check for empty fields) as when the form reappears it remains empty. Yet it seems to ignore the second set of IF statements (Connect and Insert data) as the database is not updated and the confirmation or erro messages arent displayed

The connection file mysql_connect.php is located outside of the public_html folder in a folder called private
the file structure is

server
-->public_html
--> web content files
-->private
-->mysql_connect.php

Code: Select all

<?php # Register.php

$page_title = 'Star Racing Registration';

include ('./header.inc');
?>
<BR><BR> 
<FONT FACE="Tahoma"><B>
<div id="navcontainer">
<ul id="navlist">
<li>| <A HREF="index.php">Home</a></li> |
<li><A HREF="about.php"> About Us</a></li> |
<li><A HREF="tips.php"> Tips</a></li> |
<li><A HREF="past.php"> Past Results</a></li> |
<li><A HREF="account.php"> My Account</A></li> | 
<li><A HREF="faq.php"> FAQ</A></li> | 
<li><A HREF="links.php">Links</A></li> | 
</ul>
</div>
<?php
if (isset($_POST['submit']))
 { // Handle Form

		$message = NULL; //Create Empty message
	
	if (empty($_POST['username']))
		{ $un = FALSE;
		  $message = '<P><FONT COLOR="red">You Need to enter your Username!!</P></FONT>';
		}
		else {
			$un = $_POST['username'];
		}	

	if (empty($_POST['password']))
		{ $pw = FALSE;
		  $message = '<P><FONT COLOR="red">You Need to 
			enter your Password!!</P></FONT>';
		}
		else {
			if ($_POST['password'] == $_POST['confirmpass'])
				{
				$pw = $_POST['password'];
				}
				else {
					$pw = FALSE;
					$message = '<P><FONT COLOR="red">Your Password did not match!! Please try again</P></FONT>';
				     }
		}

	if (empty($_POST['first_name']))
		{ $first = FALSE;
		  $message = '<P><FONT COLOR="red">You Need to 
			enter your First name!!</P></FONT>';
		}
		else {
			$first = $_POST['first_name'];
		}
	
	if (empty($_POST['last_name']))
		{ $last = FALSE;
		  $message = '<P><FONT COLOR="red">You Need to enter your Last name!!</P></FONT>';
		}
		else {
			$last = $_POST['last_name'];
		}

	if (empty($_POST['address']))
		{ $add = FALSE;
		  $message = '<P><FONT COLOR="red">You Need to 
			enter your Address!!</P></FONT>';
		}
		else {
			$add = $_POST['address'];
		}

	if (empty($_POST['email']))
		{ $em = FALSE;
		  $message = '<P><FONT COLOR="red">You Need to 
			enter your Email Address!!</P></FONT>';
		}
		else {
			$em = $_POST['email'];
		}

}

if ($un && $pw && $first && $last && $add && $em)
{
	//Register the user

	require_once ('../mysql_connect.php');

	//Database Query
	$query = "INSERT INTO Customers (username, password, first_name, last_name, address, email, reg_date) VALUES ('$un', PASSWORD('$pw'), '$first', '$last', '$add', '$em', NOW())";
	$result = @mysql_query ($query); //Execute
	if ($result)
		{
			echo '<P><B>Congratulations you are now registered! <P>';
			echo '<P><B>We have sent an Email to $em to confirm your registration details with us<P><P>';
			include ('./footer.inc');
			exit();
		}
		else
		{
			//If we get problems 
			$message = '<P><B>You could not be registered because of a system error.</P><P>' . mysql_error() . '</P>';
			include ('./footer.inc');
			
			
		}	
			mysql_close();

		if (isset($message))
		{
			echo '<FONT COLOR="red">' , $message, '</FONT>';
		}
}

?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" METHOD="POST">

<TABLE WIDTH=200 ALIGN=center BORDER=0 CELLSPACING=10 CELLPADDING=15>
<TR>
<TD><B>Username :</TD>
<TD><INPUT TYPE="text" NAME="Username" SIZE="15" MAXLENGTH="40" value="<?php if (isset($_POST['username'])); ?>"></TD></TR>
<TR>
<TD><B>Password :</TD>
<TD><INPUT TYPE="password" NAME="password" SIZE="15" MAXLENGTH="20"></TD></TR>
<TR>
<TD><B>Re-type Password :</TD>
<TD><INPUT TYPE="password" NAME="confirmpass" SIZE="15" MAXLENGTH="20"></TD></TR>
<TR>
<TD><B>FirstName :</TD> 
<TD><INPUT TYPE="text" NAME="first_name" SIZE="15" MAXLENGTH="40" value="<?php if (isset($_POST['first_name'])); ?>"></TD></TR>
<TR>
<TD><B>Surname :</TD> 
<TD><INPUT TYPE="text" NAME="last_name" SIZE="15" MAXLENGTH="40" value="<?php if (isset($_POST['last_name'])); ?>"></TD></TR>
<TR>
<TD><B>Address :</TD>
<TD><TEXTAREA NAME="address" ROWS=5 COLUMNS=20 WRAP ALIGN="top" value="<?php if (isset($_POST['address'])); ?>"></TEXTAREA></TD></TR>
<TR>
<TD><B>Email :</TD> 
<TD><INPUT TYPE="text" NAME="email" SIZE="40" MAXLENGTH="100" value="<?php if (isset($_POST['email'])); ?>"></TD></TR>
<TR><TD COLSPAN=2>
<FONT SIZE=2> <B>Please Check the box to indicate you have read and understood the <A HREF="terms.html" TARGET="_blank">Terms and Conditions</A>
 of using Star Racing <INPUT TYPE="checkbox" NAME="terms" Value="checked"></TD></TR>
<TR>
<TD><INPUT TYPE="submit" VALUE="Register"></TD>
<TD><INPUT TYPE="reset" VALUE="Clear Form"></TD></TR>
</TABLE>

<?php include ('./footer.inc'); ?>

Posted: Wed Nov 15, 2006 10:58 am
by evilchris2003
update

the file structure posted seems confusing when viewed so here it is again

public_html
-->web content files
private
--> mysql_connect.php

Posted: Wed Nov 15, 2006 10:59 am
by JayBird

Code: Select all

if (isset($_POST['submit']))
That wont equate to TRUE because you haven't named your Submit button

Posted: Wed Nov 15, 2006 11:01 am
by evilchris2003
damn im an idiot thanks

Posted: Wed Nov 15, 2006 11:12 am
by evilchris2003
ive just named the submit button

Code: Select all

<TD><INPUT TYPE="submit" name="submit" VALUE="Register"></TD>
and im getting the same result as before whether this is becasue the server is slow on the update (multiple mirrors) im not sure

but if you have any other suggestions it is much appreciated

Re: Problem inserting values into a database

Posted: Wed Nov 15, 2006 11:44 am
by volka
evilchris2003 wrote:as far as i can tell (well and educated guess really) is that the script runs the 1st set of IF statements (to check for empty fields) as when the form reappears it remains empty. Yet it seems to ignore the second set of IF statements (Connect and Insert data) as the database is not updated and the confirmation or erro messages arent displayed
a debugger would help you not to rely on guessing ;)
You might want to use the well-hated printf debugger, meaning: add more debug output to your script. Worst part of this solution: you have to remove or disable the debug output afterwards.

e.g.

Code: Select all

<?php # Register.php

function addDebugMessage(&$var, $text) {
	$var .= "\n<fieldset><legend>Debug</legend>" . htmlentities($text) . "</fieldset>\n";
}


$page_title = 'Star Racing Registration';

include ('./header.inc');
?>
--html--
<?php
if (isset($_POST['submit']))
{ // Handle Form

	$message = ''; //Create Empty message

	if (empty($_POST['username'])) {
		$un = FALSE;
		$message .= '<P><FONT COLOR="red">You Need to enter your Username!!</P></FONT>';
	}
	else {
		$un = $_POST['username'];
		addDebugMessage($message, '$un = '.$_POST['username']);
	}       

	if (empty($_POST['password'])) {
		$pw = FALSE;
		$message .= '<P><FONT COLOR="red">You Need to enter your Password!!</P></FONT>';
	}
	else {
		addDebugMessage($message, '!emtpy($_POST[password]');
		if ($_POST['password'] == $_POST['confirmpass']) {
			$pw = $_POST['password'];
			addDebugMessage($message, '$_POST[password] == $_POST[confirmpass])');
		}
		else {
			$pw = FALSE;
			$message .= '<P><FONT COLOR="red">Your Password did not match!! Please try again</P></FONT>';
		}
	}
a "real" debugger is better.

Posted: Wed Nov 15, 2006 11:56 am
by evilchris2003
i wasnt aware of these debuggers ill have a look on google for them thanks

Posted: Wed Nov 15, 2006 12:09 pm
by evilchris2003
right ive downloaded the gubed debugger i dont have time to check all this right now
but will check in later if i have time or most likely tomorrow

many thanks for your assistance hopefully i can sort it