基本上我正在尝试使用多个值更新我的数据库。输入字段处于循环中,因此我需要同时更新多个复选框和文本框。
$cityID = $_POST["cityID"];
$cityStatus = $_POST["cityStatus"];
$cityOrder = $_POST["cityOrder"];
$updateCities = $db->execute("UPDATE cities SET city_status=?, city_order=? WHERE city_ID=$cityID", array($city_status, $cityOrder));
foreach($cities as $row) :
?>
<input type="checkbox" name="cityStatus[]" value="1">
<input type="text" name="cityOrder[]" value="<?php echo $row->city_order ?>">
<input type="hidden" name="cityID" value="<?php echo $row->city_ID; ?>">
答案 0 :(得分:1)
我理解的是 1.你有3个字段的数组(cityid,cityorder,city status) 2.您正在从表单中提交所有这些值 3.你想要更新cityorder和citystatus取决于cityid
解决方案非常简单
$i=0;
foreach($_REQUEST['cityOrder']) as $cityorder){
$cityID = $_POST["cityID"][$i];
$cityStatus = isset($_POST["cityStatus"][$i])?$_POST["cityStatus"][$i]:0;
$cityOrder = $_POST["cityOrder"][$i];
$updateCities = $db->execute("UPDATE cities SET city_status=?, city_order=? WHERE city_ID=$cityID", array($city_status, $cityOrder));
$i++;
}