gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:33:02 25 4
gpt4 key购买 nike

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

mySQL 表

 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>

这是我用于 department-checkBoxes[] 的 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);
}
}

谢谢,感谢您的帮助!

最佳答案

查找表单域

<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);
}
}

对不起,我的英语不是直觉。

关于php - 如何使用 php 将两个不同的复选框值插入到一个 mysql 表的不同列中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27628017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com