我收到此错误
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>";
}
不知道为什么,谁能告诉我??