我的代码如下(必须与sql语句(UPDATE查询语句)有关,基本上当我进入浏览器并使用我知道存在于数据库中的密钥访问脚本时,我收到以下错误:
[15/04/2012 18:33:57] - exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'user_activation_key'' in C:\wamp\www\user-verify.php:53
Stack trace:
#0 C:\wamp\www\user-verify.php(53): PDOStatement->execute(Array)
#1 {main}
这是我的代码:,不确定它对重复条目的说法,因为user_activation_key列是唯一的,是的,我使用InnoDB和外键进行数据交互。
// check if key is set and alphanumeric and equals 40 chars long
// we use sha1 so it will always be 40 chars long.
if(isset($_GET['key']) && ctype_alnum($_GET['key']) && strlen($_GET['key']) == 40){
$key = trim($_GET['key']);
}
// if key isset
if(isset($key)){
try {
// connect to database
$dbh = sql_con();
// if key is of valid length and type we need to update the `user_activation_key` in the `users_status` table to NULL
// and update the `user_status`in the `users` table to 1 (tinyint)(active) based on the condition that the
// activation key can be found in the users_status.user_activation_key column and user_uid match in both users_status and users table
$stmt = $dbh->prepare("
UPDATE
users
JOIN
users_status
ON
users_status.user_activation_key = ?
SET
users.user_status = 1,
users_status.user_activation_key = NULL
WHERE
users_status.user_uid = users.user_uid");
// execute query
$stmt->execute(array($key));
if ( $stmt->rowCount() > 0 ) {
echo 'account now activated';
exit;
} else {
echo 'could not activate account at this time';
exit;
}
// close database connection
$dbh = null;
} // if any errors found log them and display friendly message
catch (PDOException $e) {
ExceptionErrorHandler($e);
require_once($footer_inc);
exit;
}
} else {
// else key not valid or set
echo '<h1>Invalid Activation Link</h1>';
$SiteErrorMessages =
"Oops! Your account could not be activated. Please recheck the link in your email.
The activation link appears to be invalid.<br /><br />
If the problem persists please request a new one <a href='/member/resend-activation-email'>here</a>.";
SiteErrorMessages();
include($footer_inc);
exit;
}
不知道为什么我会收到这个错误,知道它究竟意味着什么?
即使密钥存在于users_status
表中,它也不会执行更新。如果我输入无效密钥,则表示此时无法激活帐户这应该是什么,但是当密钥有效时,它应该更新,但它会输出上述错误。
更新
以下是这两个表的数据库设计。
CREATE TABLE `users` (
`user_uid` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'users unique id',
`user_status` tinyint(1) unsigned NOT NULL COMMENT '0 = verify | 1 = active | 2 = suspended | 3 = delete | 4 = spam |',
`user_login` varchar(15) NOT NULL COMMENT 'users login username',
`user_pass` char(152) NOT NULL,
`user_email` varchar(255) NOT NULL COMMENT 'users email',
`user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'user registration date',
`user_display_name` varchar(60) NOT NULL COMMENT 'users display name (first & last name)',
`user_failed_logins` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT 'failed login attempts',
PRIMARY KEY (`user_uid`),
UNIQUE KEY `user_login` (`user_login`),
UNIQUE KEY `user_email` (`user_email`),
KEY `user_pass` (`user_pass`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=Users Table';
CREATE TABLE `users_status` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'auto generated id',
`user_uid` int(10) unsigned NOT NULL,
`user_activation_key` char(40) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user_uid` (`user_uid`),
UNIQUE KEY `user_activation_key` (`user_activation_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='user status table, when a user registers they must first activate there account';
ALTER TABLE `users_status`
ADD CONSTRAINT `FK_user_status` FOREIGN KEY (`user_uid`) REFERENCES `users` (`user_uid`) ON DELETE CASCADE ON UPDATE CASCADE;
答案 0 :(得分:4)
在您的查询中,您将users_status.user_activation_key
设置为NULL
,我很确定,它有一个UNIQUE
索引,值NULL
必须已在表中提供。
这就是你收到错误的原因。
答案 1 :(得分:0)
您正在为NULL
传递user_status.user_activiation_key
值,似乎此字段是主键,它不能是NULL
或重复键(存在键),因此这将违反Integrity constraint violation
答案 2 :(得分:0)
多次敲我的头后我已经解决了,问题是即使user_activation_key是唯一的并且有一个索引我没有将列设置为允许NULL值因此错误。
我改变它所以它仍然是唯一的并且有一个索引但允许NULL值并且它可以工作。