在我的情况下,我做了INSERT&更新becoz我提出了一个条件,如果一个选民已经投票,它将只更新他在我的tbl_initialVote表上的信息。否则,它会插入。我该如何以更新格式执行此操作。请帮助...只是忽略变量$ voter的冗余.. thnks。
$mysqli->query("INSERT INTO `tbl_initialVote` (`studId`, `candId`)
VALUES ('".$voter."', '".$chairman."'),
('".$voter."', '".$vchairman."'),
('".$voter."', '".$secretary."'),
('".$voter."', '".$treasurer."')")
到Peterm
//this $getinfo is base on the logged in user
$getinfo = $mysqli->query("SELECT studId FROM`tbl_initialVote` WHERE studID = '".$voter."'");
if ($getinfo->num_rows > 0 ){
//here i know it got me error on this but this just i want to happen, make an update in the table if the user is already listed on my table. how could i suppose to do like this...@Peterm.
$mysqli->query(UPDATE `tbl_initialVote` set candId = VALUES('".$chairman."', '".$vchairman."', '".$secretary."', '".$treasurer."')
} else {
$mysqli->query("INSERT INTO `tbl_initialVote` (`studId`, `candId`)
VALUES ('".$voter."', '".$chairman."'),
('".$voter."', '".$vchairman."'),
('".$voter."', '".$secretary."'),
('".$voter."', '".$treasurer."')")
}
答案 0 :(得分:0)
我认为你正在寻找ON DUPLICATE KEY UPDATE
。
答案 1 :(得分:0)
答案 2 :(得分:0)
从我看到的,我仍然在努力理解它,恕我直言,您可以简单地按顺序执行DELETE
然后INSERT
您的新记录,而不关心更新。
这样你的所有代码都归结为:
$mysqli->query("DELETE FROM `tbl_initialVote` WHERE `studId`='" .$voter. "'");
$mysqli->query("INSERT INTO `tbl_initialVote` (`studId`, `candId`)
VALUES ('".$voter."', '".$chairman."'),
('".$voter."', '".$vchairman."'),
('".$voter."', '".$secretary."'),
('".$voter."', '".$treasurer."')");