我有4个复选框,在while循环中有不同的值。命名为数组。仅更新了最后一组循环复选框中的选定值,但我需要将所有选定的值(每组4个)单独更新为相应的行。
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);
}
}
}
?>
它现在每行更新不同的变量集,但现在一个是正确的值,另一个只是数组,但为什么?
答案 0 :(得分:0)
根据代码,foreach循环执行并更新size字段,但每次update语句运行时它都会覆盖旧值。因此,存储了最后一个循环复选框的值。您需要检查您的数据库结构。如果要为同一product_id存储4个不同的值,则需要创建不同的表(一对多关系)。希望这会有所帮助。
答案 1 :(得分:0)
我所要做的只是;
$size = array_shift($_POST['size']);