我有一個問題,不是檢查是否有重複,而是下面的腳本只是將行插入到mysql中,根本沒有更新。我試過一遍又一遍,但總是有重複。ON DUPLICATE KEY仍然複製插入
我究竟在做什麼錯在這裏?
if ($completeStatus == "0") {
// Get the default questions responses and insert into database
$questionsAnswered = $_GET['questions'];
foreach($questionsAnswered as $key => $answers){
$query = "INSERT INTO survey_ResponseDetail (responseHeader,
questionID,
questionText,
ansLikelihood,
ansExpConsq,
ansRepRisk,
currWayMitigate
)
VALUES ('$ResponseHeader',
'$key',
'test1',
'$answers[1]',
'$answers[2]',
'$answers[3]',
'$answers[4]'
)
ON DUPLICATE KEY UPDATE ansLikelihood = '$answers[1]',
ansExpConsq = '$answers[2]',
ansRepRisk = '$answers[3]',
currWayMitigate = '$answers[4]',
id = LAST_INSERT_ID(id)";
$dbresult = mysql_query($query) or die ('Database Error (' . mysql_errno() . ') ' . mysql_error());
}
不得不問。您在survey_ResponseDetail表上確實有PK或其他唯一約束。對? – JohnFx
我對mySQL的知識現在非常弱,所以我不知道。如果我在創建表格時必須設置它,那麼不需要。 –