我下面的代码(必须与 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;