Duplicate entry 'bla bla bla bla 'on key 'username'?
I don't know what's wrong with my code:
<!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=utf-8" />
<title>Register</title>
&l开发者_Python百科t;/head>
<body>
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("dressoholic") or die(mysql_error());
if($_POST['form_submitted'] == '1'){
$actkey = mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();
$birthdate = $_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'];
$usrname = mysql_real_escape_string($_POST['uname']);
$pswd = mysql_real_escape_string($_POST['password']);
$emaill = mysql_real_escape_string($_POST['email1']);
# gender moet nog gedaan worden!!!
$sql = "INSERT INTO users(username, fname, lname, email, password, activationkey, bdate, status) VALUES
('$usrname','$_POST[fname]','$_POST[lname]','$emaill','$pswd','$birthdate','$actkey','verify')";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "An email has been sent to $_POST[email1] with an activation key. Please check your mail to complete registration.";
##Send activation Email
$to = $_POST['email1'];
$subject = " Dressoholic.com Registration";
$message = "Welcome to our website!\r\rYou, or someone using your email address, has completed registration at Dressoholic.com. You can complete registration by clicking the following link:\rhttp://localhost/dressoholic/register.php?$activationKey\r\rIf this is an error, ignore this email and you will be removed from our mailing list.\r\rRegards,\ Dressoholic.com Team";
$headers = 'From: blabla@gmail.com' . "\r\n" .
'Reply-To: blabla@gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
else {
##User isn't registering, check verify code and change activation code to null, status to activated on success
$queryString = $_SERVER['QUERY_STRING'];
$query = "SELECT * FROM users";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
if ($queryString == $row["activationkey"]){
echo "Congratulations!" . $row["username"] . " is now the proud new owner of a Dressoholic.com account.";
$sql="UPDATE users SET activationkey = '', status='activated' WHERE (id = $row[id])";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
}
}
}
?>
</body>
</html>
This is the exact error:
Error: Duplicate entry 'arnold-blabla@gmail.c-1987--22' for key 'username'
arnold is the username I entered. blabla@gmail.com is the e-mail I entered. 1987 the year of birth and 22 the day!
I must say I am a beginner in PHP and MySQL. Thanks.
You have a unique index in your users
table on the username
column. This means that two different users cannot have the same username
.
Enter a different username or remove the index, and you should be fine.
You are trying to insert a user name that already exists (username
field) on your database. Try selecting a name different from arnold-blabla@gmail.c-1987--22
or perform an update
sentence instead of an insert
if you are just trying to update user information.
This isn't a PHP problem, this is a MySQL problem. My guess is that username
is a primary key on the table users
. If this is the case, each username must be unique. MySQL is telling you there's a duplicate, which is not allowed.
精彩评论