多选了 MySQL 数据的复选框,用逗号 (,) 分隔

Multi Checked on checkbox from MySQL data which is separated by Comma (,)

有更新表单,所以我需要它,如果某些复选框输入已经根据 MySQL 数据进行了检查,那么它将显示已检查。

<?php 
$statement = $db->prepare("SELECT leavingEquipment FROM orderform WHERE orderNo='$orderNo'");  // here are the checked data 
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
    foreach($result as $row4){
        $leavingEquipment = explode(", ",$row4['leavingEquipment']); // Here I use explode I check this with var_dump 
        $statement = $db->prepare("SELECT * FROM equipment"); // here all equipment 
        $statement->execute();
        $result = $statement->fetchAll(PDO::FETCH_ASSOC);
            foreach($result as $row3) {
                $leavingEquipment3=$row3['equipmentTitle'];
                ?>
            <input type="checkbox" name="leavingEquipment[]" value="<?php echo $row3['equipmentTitle']; ?>" <?php if($leavingEquipment==$leavingEquipment3) echo 'checked'; ?>><label class="control-label"><?php echo $row3['equipmentTitle']; ?></label>
                <?php
             }
    }
?>

Var_dum 输出如下: 数组(2) { [0]=> 字符串(9) "Bluetooth" 1=> 字符串(6) "casing" }

显示如下:

但我需要这样:

提前致谢。

看这里,

<input ... name="leavingEquipment[]" } value="<?php echo $row3['equipmentTitle']; ?>" <?php if($leavingEquipment==$leavingEquipment3) echo 'checked'; ?>> ...
                                     ^ a lone closing bracket                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

您正在比较数组 $leavingEquipment 和字符串 $leavingEquipment3

而是使用 in_array() 函数来检查复选框是否已被选中,即设备是否已在阵列中。所以你的内部 foreach 循环应该是这样的:

// your code

foreach($result as $row3) {
    $leavingEquipment3=$row3['equipmentTitle'];
    ?>
    <input type="checkbox" name="leavingEquipment[]" value="<?php echo $row3['equipmentTitle']; ?>" <?php if(in_array($leavingEquipment3, $leavingEquipment)){ echo " checked='checked'"; } ?>>
    <label class="control-label"><?php echo $row3['equipmentTitle']; ?></label>
    <?php
}

// your code