ON DUPLICATE KEY UPDATE - 当 if 语句为 true - 更新列,否则触发 1062 错误以捕获重复
ON DUPLICATE KEY UPDATE - when if statement true - update column, else trigger 1062 error to catch duplication
我是这个网站的新手,这是我的第一个 post。
我有 MySQL PDO 准备好的查询:
try {
$sql = "INSERT INTO `site_users_wishlist` (`user_id`, `uin`, `active`)
VALUES (?, ?, 'Y')";
$stmt = $pdo->prepare($sql);
$stmt->execute(array($userId,$uin));
}
catch (PDOException $e) {
if ($e->errorInfo[1] == 1062) {
echo "__item_exists_in_wishlist__";
exit;
}
else {
echo "Error adding item to wishlist!<br/>".$e;
exit;
}
}
echo "__item_added_to_wishlist__";
exit;
“active”列的值为 Y / N / B (Yes, No, B应该).
当用户将项目添加到他的心愿单时,它会使用'active'='Y'插入数据库,
当用户从心愿单中删除项目时,它变为“active”=“N”。
此时问题继续:
如果用户尝试多次添加项目,我会捕获重复条目的 1062 错误 并向用户显示消息。
如果用户之前删除了该项目,并想将其重新添加到 his/her 心愿单,我想显示它已成功添加到心愿单的消息,而不是它已经存在于数据库.
我需要一些干净的代码,比如
$sql = "INSERT INTO `site_users_wishlist` (`user_id`, `uin`, `active`)
VALUES (?, ?, 'Y')
ON DUPLICATE KEY UPDATE IF(`active`<>'Y', `active`='Y', TRIGGER DUPLICATE ERROR 1062";
提前致谢
编辑 1:
Table 创建代码:
CREATE TABLE `site_users_wishlist` (
`user_id` int(11) NOT NULL COMMENT 'user id',
`uin` int(5) NOT NULL COMMENT 'item id',
`date_added` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'date of addition by user',
`added_price` int(10) DEFAULT NULL COMMENT 'price when added',
`updated_price` int(10) DEFAULT NULL COMMENT 'current price',
`active` tinytext NOT NULL COMMENT 'N - not, Y - yes, B - bought by user',
PRIMARY KEY (`user_id`,`uin`),
KEY `user_id` (`user_id`),
CONSTRAINT `userId` FOREIGN KEY (`user_id`) REFERENCES `site_users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
好的,所以,我没有找到针对此问题的优雅 单一查询 解决方案,但这里是 3 查询 的解决方案,根据我的具体需求连3次都没问题
try {
$sql = "SELECT * FROM `site_users_wishlist` WHERE `user_id` = ? AND `uin` = ? AND `active` <> 'Y'";
$stmt = $pdo->prepare($sql);
$stmt->execute(array($userId, $uin));
$exists = $stmt->rowCount();
if ($exists == 1) {
try {
$sql2 = "UPDATE `site_users_wishlist` SET `active` = 'Y' WHERE `user_id` = ? AND `uin` = ?";
$stmt2 = $pdo->prepare($sql2);
$stmt2->execute(array($userId, $uin));
}
catch (PDOException $e) {
echo "Error adding item to wishlist!<br/>".$e;
exit;
}
}
else {
try {
$sql2 = "INSERT INTO `site_users_wishlist` (`user_id`, `uin`, `uinsql`, `added_price`, `last_updated_price`, `active`) VALUES (?, ?, ?, ?, ?, 'Y')";
$stmt2 = $pdo->prepare($sql2);
$stmt2->execute(array($userId, $uin, $uinsql, $price, $price));
}
catch (PDOException $e) {
if ($e->errorInfo[1] == 1062) { // 1062 error code for duplicate entry
echo "__item_exists_in_wishlist__";
exit;
}
else {
echo "Error adding item to wishlist!<br/>".$e;
exit;
}
}
}
}
catch (PDOException $e) {
echo "Error adding item to wishlist!<br/>".$e;
exit;
}
echo "__item_added_to_wishlist__";
exit;
也许有一天它会对其他人有所帮助 ;)
我是这个网站的新手,这是我的第一个 post。
我有 MySQL PDO 准备好的查询:
try {
$sql = "INSERT INTO `site_users_wishlist` (`user_id`, `uin`, `active`)
VALUES (?, ?, 'Y')";
$stmt = $pdo->prepare($sql);
$stmt->execute(array($userId,$uin));
}
catch (PDOException $e) {
if ($e->errorInfo[1] == 1062) {
echo "__item_exists_in_wishlist__";
exit;
}
else {
echo "Error adding item to wishlist!<br/>".$e;
exit;
}
}
echo "__item_added_to_wishlist__";
exit;
“active”列的值为 Y / N / B (Yes, No, B应该).
当用户将项目添加到他的心愿单时,它会使用'active'='Y'插入数据库, 当用户从心愿单中删除项目时,它变为“active”=“N”。
此时问题继续:
如果用户尝试多次添加项目,我会捕获重复条目的 1062 错误 并向用户显示消息。
如果用户之前删除了该项目,并想将其重新添加到 his/her 心愿单,我想显示它已成功添加到心愿单的消息,而不是它已经存在于数据库.
我需要一些干净的代码,比如
$sql = "INSERT INTO `site_users_wishlist` (`user_id`, `uin`, `active`)
VALUES (?, ?, 'Y')
ON DUPLICATE KEY UPDATE IF(`active`<>'Y', `active`='Y', TRIGGER DUPLICATE ERROR 1062";
提前致谢
编辑 1:
Table 创建代码:
CREATE TABLE `site_users_wishlist` (
`user_id` int(11) NOT NULL COMMENT 'user id',
`uin` int(5) NOT NULL COMMENT 'item id',
`date_added` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'date of addition by user',
`added_price` int(10) DEFAULT NULL COMMENT 'price when added',
`updated_price` int(10) DEFAULT NULL COMMENT 'current price',
`active` tinytext NOT NULL COMMENT 'N - not, Y - yes, B - bought by user',
PRIMARY KEY (`user_id`,`uin`),
KEY `user_id` (`user_id`),
CONSTRAINT `userId` FOREIGN KEY (`user_id`) REFERENCES `site_users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
好的,所以,我没有找到针对此问题的优雅 单一查询 解决方案,但这里是 3 查询 的解决方案,根据我的具体需求连3次都没问题
try {
$sql = "SELECT * FROM `site_users_wishlist` WHERE `user_id` = ? AND `uin` = ? AND `active` <> 'Y'";
$stmt = $pdo->prepare($sql);
$stmt->execute(array($userId, $uin));
$exists = $stmt->rowCount();
if ($exists == 1) {
try {
$sql2 = "UPDATE `site_users_wishlist` SET `active` = 'Y' WHERE `user_id` = ? AND `uin` = ?";
$stmt2 = $pdo->prepare($sql2);
$stmt2->execute(array($userId, $uin));
}
catch (PDOException $e) {
echo "Error adding item to wishlist!<br/>".$e;
exit;
}
}
else {
try {
$sql2 = "INSERT INTO `site_users_wishlist` (`user_id`, `uin`, `uinsql`, `added_price`, `last_updated_price`, `active`) VALUES (?, ?, ?, ?, ?, 'Y')";
$stmt2 = $pdo->prepare($sql2);
$stmt2->execute(array($userId, $uin, $uinsql, $price, $price));
}
catch (PDOException $e) {
if ($e->errorInfo[1] == 1062) { // 1062 error code for duplicate entry
echo "__item_exists_in_wishlist__";
exit;
}
else {
echo "Error adding item to wishlist!<br/>".$e;
exit;
}
}
}
}
catch (PDOException $e) {
echo "Error adding item to wishlist!<br/>".$e;
exit;
}
echo "__item_added_to_wishlist__";
exit;
也许有一天它会对其他人有所帮助 ;)