我不知道我的代码有什么问题:
<!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>
</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>
这是确切的错误:
Error: Duplicate entry 'arnold-blabla@gmail.c-1987--22' for key 'username'
arnold是我输入的用户名。 blabla@gmail.com是我输入的电子邮件。 1987年出生的那一年和22天!
我必须说我是PHP和MySQL的初学者。感谢。
答案 0 :(得分:2)
users
列的username
表格中有唯一索引。这意味着两个不同的用户不能拥有相同的username
。
输入不同的用户名或删除索引,你应该没问题。
答案 1 :(得分:1)
您正在尝试在数据库中插入已存在的用户名(username
字段)。如果您只是尝试更新用户信息,请尝试选择与arnold-blabla@gmail.c-1987--22
不同的名称或执行update
句子而不是insert
。
答案 2 :(得分:1)
这不是PHP问题,这是一个MySQL问题。我的猜测是username
是表users
上的主键。如果是这种情况,则每个用户名必须是唯一的。 MySQL告诉你有重复,这是不允许的。