php - 键 'bla bla bla bla ' 上的重复条目 'username'?

标签 php mysql database

我不知道我的代码有什么问题:

<!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 的初学者。谢谢。

最佳答案

您在 users 表的 username 列中有一个唯一索引。这意味着两个不同的用户不能有相同的用户名

输入不同的用户名或删除索引,应该没问题。

关于php - 键 'bla bla bla bla ' 上的重复条目 'username'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4532392/

相关文章:

php - 在 Javascript 中解析一个单独的 PHP 数组

mysql - 将数组合并和排序为一个的公式/算法

PHP - 从数据库加载/删除是否使用静态方法?

mysql - 数据库中的“ts”数字字段? (例如 1428881039)

php - 将整个 MySQL 表放入 PHP 数组 VS 逐行读取

php - 比较,!== 与 !=

php - 使用 Eloquent 复杂查询 - Laravel 4

php - Mysql查询不适用于有空间的查询

php - 通过链接进行 SQL 注入(inject)?

angularjs - 将 angularjs 和 node.js 后端与 Cloudant DB 搜索 API 结合使用