0
我收到此錯誤PDO完整性約束違規
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 1
在我的表,我沒有主鍵與0和echo $item['nid']
沒有0值也是如此。
$insertTotalRes = $conWS->prepare('INSERT INTO totalresponses (nid, created) VALUES(:nid, :created) ON DUPLICATE KEY UPDATE nid = :nid , created = :created ');
$conWS->beginTransaction();
try{
foreach ($output as $key=>$item){
$insertTotalRes->bindParam(':nid', $item['nid']);
$insertTotalRes->bindParam(':created', $item['created']);
$insertTotalRes->execute();
}
$conWS->commit();
}catch (PDOException $e){
$conWS->rollback();
echo "FAIL TO ADD ".$e->getMessage()."<br>";
}
但用這種方法我沒有得到錯誤。
$conWS->beginTransaction();
try{
foreach ($output as $key=>$item){
$insertTotalRes = $conWS->prepare('INSERT INTO totalresponses (nid, created) VALUES('.$item['nid'].', '.$item['created'].') ON DUPLICATE KEY UPDATE nid = '.$item['nid'].' , created = '.$item['created'].' ');
$insertTotalRes->execute();
}
$conWS->commit();
}catch (PDOException $e){
$conWS->rollback();
echo "FAIL TO ADD ".$e->getMessage()."<br>";
}
我不知道爲什麼,任何人都可以告訴我?
你是對的,謝謝你的幫助。 – Eric
@Eric不客氣,順便說一句,如果一個答案是有幫助的,你可以upvote它:)只是說這個,因爲從我之前看到的一些早期的問題,接受的答案大多有零分:) –