MySQL Insert...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lostprophetpunk
Forum Newbie
Posts: 21
Joined: Sat May 31, 2008 3:49 am

MySQL Insert...

Post 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.
pkbruker
Forum Commoner
Posts: 32
Joined: Sun Aug 03, 2008 9:36 am
Location: Oslo, Norway

Re: MySQL Insert...

Post 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.
User avatar
chaos
Forum Newbie
Posts: 22
Joined: Thu May 15, 2008 9:20 am
Location: New Jersey

Re: MySQL Insert...

Post 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.
Post Reply