如何使用PHP将带有复选框的URL从URL插入数据库?

时间:2015-01-22 02:21:39

标签: php mysql checkbox

我正在尝试从这样的网址获取ID :( 37是ID)

http://localhost/myQSP/public/assign-qsps.php?employeeName=37

并将其与复选框选择一起插入数据库。

数据库

id | employees_fk | qsps_fk | checked
1       37            101        1
2       37            102        1

qsps_fk是一个关系表。使用此流程代码

<?php
if (isset($_POST['submit'])) {

    $assign_qsps_id = $_POST["hidden-qsp-checkBoxes"];
    $hidden_qsp = $_POST["department-qsp-checkBoxes"];

    if(isset($_POST['hidden-qsp-checkBoxes'])) {

        foreach ($assign_qsps_id as $qsp_id){ 

        $visible_assign_qsp = in_array($qsp_id, $hidden_qsp) ? 1 : 0;

        $query = "INSERT INTO junction_employees_qsps (qsps_fk, checked) VALUES ($qsp_id, $visible_assign_qsp);
        $result = mysqli_query($db_connection, $query);
        }
    }
}
?>

它会创建一个像这样的表

id | employees_fk | qsps_fk | checked
1        0            101        1
2        0            102        1

我的表单是

<form action="assign-qsps-process.php" method="post">
<input type="submit" name="submit" value="ASSIGN QSP'S" />
<ul>
    <?php $employee_qsps = find_departments_for_assign_qsps($current_employee['id']);

while($qsp = mysqli_fetch_assoc($employee_qsps)) { ?>
        <li>

        <input type='checkbox' name='hidden-qsp-checkBoxes[]' value='<?php htmlentities($qsp["qsp_id"]); ?>' style='display:none' checked='checked' />"; 

        <input type='checkbox' name='department-qsp-checkBoxes[]' value='<?php htmlentities($qsp["qsp_id"]); ?>' />

        <?php echo htmlentities($qsp['qsp_name']); ?>                       
       </li>
    }
    </ul>

</form>

那么如何从带有复选框选择的URL中获取ID以及使employees_fk成为关系表?

我已经尝试了这个并且它不起作用。

<?php
if (isset($_POST['submit'])) {

    $id_employee = $current_employee["id"];
    $assign_qsps_id = $_POST["hidden-qsp-checkBoxes"];
    $hidden_qsp = $_POST["department-qsp-checkBoxes"];

    if(isset($_POST['hidden-qsp-checkBoxes'])) {

        foreach ($assign_qsps_id as $qsp_id){ 

        $visible_assign_qsp = in_array($qsp_id, $hidden_qsp) ? 1 : 0;

        $query = "INSERT INTO junction_employees_qsps (employees_fk, qsps_fk, checked) VALUES ($id_employee, $qsp_id, $visible_assign_qsp);
        $result = mysqli_query($db_connection, $query);
        }
    }
}
?>

谢谢!我非常感谢帮助!

0 个答案:

没有答案