【发布时间】:2017-10-15 08:16:27
【问题描述】:
我在一个 while 循环中有 4 个具有不同值的复选框并命名为一个数组。仅从最后循环的一组复选框中选择的值正在更新,但我需要将所有选定的值(每组 4 个 1 个)独立更新到它们对应的行。
HTML
<input type="checkbox" name="size[]" value="S" />
<input type="checkbox" name="size[]" value="M" />
<input type="checkbox" name="size[]" value="L" />
<input type="checkbox" name="size[]" value="XL" />
PHP
<?php
if(isset($_POST['update'])){
$select_id = "select * from customer where ip='$ip'";
$get_id = mysqli_query($db, $select_id);
while ($row = mysqli_fetch_array($get_id)){
foreach($_POST['size'] as $size){
$product_id = $row['product_id'];
$update_size = "update customer set size='$size' where product_id='$product_id'";
$run_size = mysqli_query($db, $update_size);
}
}
}
?>
更新 - 快到了!
退出循环
$i = 1;
在循环中
$i++;
<input type="checkbox" name="size[].$i." value="S" />
<input type="checkbox" name="size[].$i." value="M" />
<input type="checkbox" name="size[].$i." value="L" />
<input type="checkbox" name="size[].$i." value="XL" />
处理中
<?php
if(isset($_POST['update'])){
$select_id = "select * from customer where ip='$ip'";
$get_id = mysqli_query($db, $select_id);
while ($row = mysqli_fetch_array($get_id)){
$size = $_POST['size'];
$product_id = $row['product_id'];
foreach ($size as $_POST['size']);
{
$update_size = "update customer set size='$size' where product_id='$product_id'";
$run_size = mysqli_query($db, $update_size);
}
}
}
?>
现在每行更新不同的变量集,但现在一个是正确的值,另一个只是数组,但为什么?
【问题讨论】: