Php 我能';似乎没有给db添加复选框值

Php 我能';似乎没有给db添加复选框值,php,Php,我正在从sql值创建几个复选框 <?php $i=0; $query5= "SELECT * FROM sales_price WHERE product_id = $currentid"; $choices5 = $conn->query($query5); foreach($choices5 as $choice5){ ?> <input type="checkbox&quo

我正在从sql值创建几个复选框

<?php
    $i=0;
    $query5= "SELECT * FROM sales_price WHERE product_id = $currentid";
    $choices5 = $conn->query($query5);
        foreach($choices5 as $choice5){
        ?>
    <input type="checkbox" id="gift<?php echo $i; ?>" name="gift[<?php echo $i; ?>]['gft']" value="<?php echo $choice5['price_name']; ?>">
    <label for="gift<?php echo $i; ?>" style="margin-right:15px"><?php echo $choice5['price_name']; ?></label>
     <?php $i++; 
    }  ?>

例如:

<input type="checkbox" id="gift1" name="gift[1]['gft']" value="small">
<label for="gift1" style="margin-right:15px">small</label>
<input type="checkbox" id="gift2" name="gift[2]['gft']" value="medium">
<label for="gift2" style="margin-right:15px">medium</label>
<input type="checkbox" id="gift3" name="gift[3]['gft']" value="large">
<label for="gift3" style="margin-right:15px">large</label>

小的
中等的
大的
整个东西都在一个
我正在尝试将选中的值添加到具有

foreach($_POST['gift'] as $gifts){
$stmt = $conn->prepare('INSERT INTO cart_gifts(cart_id, gift) VALUES (:ucartid, :ugift)');
$stmt->bindParam(':ucartid', $last_id);
$stmt->bindParam(':ugift', $gifts['gft']);
if($stmt->execute()){   
echo "<script>window.history.back()</script>";
}
}
foreach($\u POST['gift']作为$gifts){
$stmt=$conn->prepare('插入购物车礼物(购物车id,礼物)值(:ucartid,:ugift)');
$stmt->bindParam(':ucartid',$last\u id);
$stmt->bindParam(':ugift',$gifts['gft']);
如果($stmt->execute()){
回显“window.history.back()”;
}
}

但我面临着以下问题,代码确实会循环输入3行(如示例所示)到表中,列ID(A.I.)、Cart_ID、gift、,它将ID和ofc添加到购物车ID中,但礼品列保持为空,并且如果仅选中一个复选框,则仍会输入所有三个复选框。

保持每个复选框输入的名称相同

<?php
$i=0;
$query5= "SELECT * FROM sales_price WHERE product_id = $currentid";
$choices5 = $conn->query($query5);
    foreach($choices5 as $choice5){
    ?>
<input type="checkbox" id="gift<?php echo $i; ?>" name="gift[]" value="<?php echo $choice5['price_name']; ?>">
<label for="gift<?php echo $i; ?>" style="margin-right:15px"><?php echo $choice5['price_name']; ?></label>
 <?php $i++; 
}  
?>

它就像一个符咒,我有一个小问题,但由于我的循环,我修复了它。非常感谢你。
    <input type="checkbox" id="gift1" name="gift[]" value="small">
    <label for="gift1" style="margin-right:15px">small</label>
    <input type="checkbox" id="gift2" name="gift[]" value="medium">
    <label for="gift2" style="margin-right:15px">medium</label>
    <input type="checkbox" id="gift3" name="gift[]" value="large">
    <label for="gift3" style="margin-right:15px">large</label>
$stmt->bindParam(':ugift', $gifts);