Page 1 of 1

MySQL Insert...

Posted: Wed Aug 06, 2008 1:43 pm
by lostprophetpunk
I was wondering how I could check if values have been inserted into the database table successfully?

Also, my register code doesn't seem to work as it comes up with the blue error below the insert statement...

Code: Select all

<?php
 
require_once('include.php');
 
$error = '';
$form = $_POST['submit'];
$username = mysql_real_escape_string( $_POST['username'] );
$password = mysql_real_escape_string( $_POST['password'] );
$email = mysql_real_escape_string( $_POST['email'] );
 
if( isset($form) ) {
if( isset($username) && isset($MD5password) && $username !== '' && $password !== '' ) {
 
$MD5password =  md5( $password );
 
$MD52password = md5( $MD5password );
 
$sql = mysql_query("INSERT INTO example 
(username, password, email) VALUES('$username', '$MD52password', '$email' ) ");
 
} else { $error = 'All information is not filled out correctly';}
} 
 
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Register</title>
</head>
<body>
 
<form action="<?php $PHP_SELF; ?>" method="post" >
Username:
<input name="username" type="text" value="<?php echo "$username";?>" /><br />
Password:
<input name="password" type="password" /><br />
Email:
<input name="email" type="text" value="<?php echo "$email";?>" /><br />
<input name="submit" type="submit" value="Register" />
</form>
 
<?php
 
echo "<br /><span style=\"color:blue\">$error</span>";
 
?>
 
</body>
</html>
This is really annoying me now.

Thanks in advance for the help.

Re: MySQL Insert...

Posted: Wed Aug 06, 2008 1:54 pm
by pkbruker
1. Your error:
Do you connect to the database somewhere? If not, your query will definitely fail!

2. Check if inserted
If mysql_query with an insert statement does not return a false value, everything should (generally) be OK. But, if you wanna be sure, check out the mysql_affected_rows function.

Re: MySQL Insert...

Posted: Wed Aug 06, 2008 2:05 pm
by chaos
On line 12, you mean:

Code: Select all

if( isset($username) && isset($password) && $username !== '' && $password !== '' ) {
'

Presently, you're checking for whether $MD5password is set before it possibly could be.