[英]SQL Statement Giving me an error?
我的以下代碼(必須與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;
在查詢中,您將users_status.user_activation_key
設置為NULL
,我很確定,它具有UNIQUE
索引,表中必須已經有NULL
值。
這就是為什么您收到該錯誤的原因。
您正在傳遞user_status.user_activiation_key
的NULL
值,並且此字段似乎是主鍵,不能為NULL
或重復鍵(存在鍵),因此這將違反Integrity constraint violation
在我幾次解決問題之后,問題還是出在哪,即使您user_activation_key是唯一的並且具有一個索引,但我沒有將列設置為允許NULL值,因此出現了錯誤。
我對其進行了更改,使其仍然是唯一的,並且具有索引,但允許使用NULL值,並且可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.