问题
I know what the error means, and why it's occurring, meaning this is not a duplicate of the many topics covering State 23000. I'm using PDO to insert a duplicate record into a table that has a compound PK. I did this on purpose, in order to automatically cancel the insertion of any duplicated records in to this specific table.
The error, SQLSTATE[23000]: Integrity constraint violation
, is killing the script. Is there a way to set the warn level for this particular error, to literally tell it to shut up and carry on?
I don't understand the purpose of having compound PKs if we can't allow them to work for us?
I'm not very experienced with DB administration so you'll have to forgive me if I come across a bit 'it should work if I hit it with a hammer'.
Table Structure:
CREATE TABLE `cart` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(10) NOT NULL,
`itemid` int(10) NOT NULL,
UNIQUE KEY `id` (`id`),
UNIQUE KEY `unique_records` (`userid`,`itemid`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1
Note the compound PK called 'unique records'.
so I have this table:
| id | userid | itemid |
| 1 | 175 | 12 |
And I execute this query:
$insertItem = $db->query("
INSERT INTO cart (userid, itemid)
VALUES (:userid, :itemid),
array("userid"=>"175", "itemid"=>"12")
");
Executing this query prompts this error: SQLSTATE[23000]: Integrity constraint violation
.
I fully understand why I'm getting this error - I just don't understand why it's killing my submission? I thought it should just keep working ignoring the fact that a few records weren't inserted because they were duplicates?
Advice appreciated!
回答1:
If you want the insert
to keep working, you can try using INSERT IGNORE
:
INSERT IGNORE INTO cart(userid, itemid)
VALUES (:userid, :itemid);
INSERT IGNORE
can be a bit strong because it ignores all errors. If you just want to ignore duplicate key errors, use on duplicate key
and set a value to itself:
INSERT IGNORE INTO cart(userid, itemid)
VALUES (:userid, :itemid)
ON DUPLICATE KEY userid = values(userid);
The set
part does nothing -- the value doesn't change -- but the effect is to ignore the error.
回答2:
Your question appears to be quite vague - either you want to ignore your integrity constraint violations, or you want them to be a signal saying "no more inserts".
In addition to the answer with insert ignore
solution - when you execute your query and get SQLSTATE[23000]: Integrity constraint violation
error, that means PDO throws exception. If you don't catch
it, your script is killed. If you don't want it to be killed, wrap your DB code into try-catch construct, i.e.:
try {
// code here
$insertItem = $db->query("
INSERT INTO cart (userid, itemid)
VALUES (:userid, :itemid)",
array("userid"=>"175", "itemid"=>"12")
);
// more code here
} catch(Exception $e) {
// handle exception -
// find out if it is caused by integrity contraints violations
// and if it is - merely go further
// otherwise do something else, like re-throwing your exception
}
HTH
来源:https://stackoverflow.com/questions/23974072/sqlstate23000-integrity-constraint-violation-error-handling