如何使用 php 将两个不同的复选框值插入到一个 mysql 表的不同列中


How to insert two different check box values into different columns of one mysql table using php?

好的,我正在尝试使用文本字段和复选框将一些数据插入mySQL。 我让文本字段转到一个表,复选框转到另一个表。 我可以获取要插入到两个表中的数据,但我正在尝试使第二个复选框根据选择的部门将数据插入可见列。 我的想法是隐藏部门复选框[]并显示可见复选框[],只要我能插入可见复选框[]数据。

mySQL Tabel

 id | departments_fk | qsps_fk | visible
270 |             1  |    218  |     0
271 |            22  |    218  |     0
272 |             1  |    219  |     0
273 |            22  |    219  |     0
274 |             1  |    220  |     0
275 |            22  |    220  |     0

这是我的表格。

    <form action="create-qsp.php" method="post">
      <div class="qsp-name">QSP Name:
        <input type="text" name="qsp_name" class="name-box" value="" />
      </div>
    <div class="rev">QSP Rev:
        <input type="text" name="qsp_rev" class="rev-box" value="" />
      </div>
       <div class="qsp-departments">Department:</div>
      <div class='department-checkBoxes'>
    <label class='checkBoxes'>
    <?php $qsp_department_list = find_all_departments();
        while($qsp_department = mysqli_fetch_assoc($qsp_department_list)) { global $visible;?>
            <div class='department-check'>
            <input type='checkbox' name='department-checkBoxes[]' value='
            <?php echo htmlentities($qsp_department["id"]); ?>
            ' checked='checked' />  
            <input type='checkbox' name='visible-checkBoxes[]' value='1' /> 
            <?php echo htmlentities($qsp_department["department_name"]); ?>
            </div>
        <?php }
    mysqli_free_result($qsp_department_list); ?>
    </label>
    </div>
     <div class="create-btn">
     <input type="submit" name="submit" value="Create QSP" />
     </div>
    </form>

这是我的 PHP 部门复选框[]

$qsps_id = mysqli_insert_id($db_connection);
$departments_id = $_POST["department-checkBoxes"];
$visible_id = $_POST["visible-checkBoxes"];

if(isset($_POST['department-checkBoxes'])) {
    foreach ($departments_id as $id){ 
        $query1 = "INSERT INTO junction_departments_qsps (departments_fk, qsps_fk) VALUES ($id, $qsps_id) ";
        $result1 = mysqli_query($db_connection, $query1);
     }
 }      

这是我的 PHP 用于可见复选框[]

if(isset($_POST['visible-checkBoxes'])) {
    foreach ($visible_id as $id){ 
        $query2 = "INSERT INTO junction_departments_qsps (qsps_fk, visible) VALUES ($qsps_id, $id) ";
        $result2 = mysqli_query($db_connection, $query2);
     }
 }  

谢谢,我感谢你的帮助!

finde form field

<input type='checkbox' name='visible-checkBoxes[]' value='1' />

编辑

<input type='checkbox' name='visible-checkBoxes[]' value='<?php echo $qsp_department["id"]; ?>' />

并编辑 php 代码

if(isset($_POST['department-checkBoxes'])) {
    foreach ($departments_id as $id){ 
        $visible = in_array($id,$_POST['visible-checkBoxes']) ? 1 : 0;
        $query1 = "INSERT INTO junction_departments_qsps (departments_fk, qsps_fk, visible) VALUES ($id, $qsps_id,$visible) ";
        $result1 = mysqli_query($db_connection, $query1);
     }
 } 

删除代码

if(isset($_POST['visible-checkBoxes'])) {
    foreach ($visible_id as $id){ 
        $query2 = "INSERT INTO junction_departments_qsps (qsps_fk, visible) VALUES ($qsps_id, $id) ";
        $result2 = mysqli_query($db_connection, $query2);
     }
 } 

我的英语不是直觉。