Php 如何用多行值更新一个表
在我的例子中,我做了插入和更新,因为我提出了一个条件,如果选民已经投票,它将只更新我的tbl_initialVote表上的他的信息。否则,它将插入。如何以更新格式执行此操作。请帮忙。。。只需忽略变量$voter的冗余。。thnksPhp 如何用多行值更新一个表,php,mysql,Php,Mysql,在我的例子中,我做了插入和更新,因为我提出了一个条件,如果选民已经投票,它将只更新我的tbl_initialVote表上的他的信息。否则,它将插入。如何以更新格式执行此操作。请帮忙。。。只需忽略变量$voter的冗余。。thnks $mysqli->query("INSERT INTO `tbl_initialVote` (`studId`, `candId`) VALUES ('".$voter."', '".$chairman."'),
$mysqli->query("INSERT INTO `tbl_initialVote` (`studId`, `candId`)
VALUES ('".$voter."', '".$chairman."'),
('".$voter."', '".$vchairman."'),
('".$voter."', '".$secretary."'),
('".$voter."', '".$treasurer."')")
彼得
//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."')")
}
我想您正在寻找。谷歌搜索了“使用多行值php更新表” 产生了这个结果: 我希望这有帮助,兄弟
Alex据我所见,我仍在努力理解它,我想你可以简单地依次执行
删除
,然后插入你的新记录,而不必关心更新
这样,您的所有代码都可以归结为:
$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."')");
你能给我举个例子吗?基于您的链接,我无法了解我如何做到这一点。请您详细说明一下您的要求,并用一些示例数据加以说明,包括所需的输出?@peterm。。我更新了我发布的代码。。希望你能给我一个答案。。提前通知……对不起,没有说得更清楚。tbl\u initialVote
表是否有其他列?插入数据后,您如何区分董事长和司库?