如何让我的复选框循环单独更新每个复选框,而不是 php 和 mySQL 中的两个复选框?
How to get my checkbox loop to update each checkbox individually instead of both of them in php and mySQL?
我可以让复选框更新,但它们不能完全正常工作。当两个框都为空时,我可以选中其中一个,并且两个框都会更新为选中状态。如果两个框都被选中,我必须取消选中两个框才能将它们更新为未选中。我无法让一个框被选中而另一个框被取消选中。
有什么想法吗?
非常感谢您的帮助。
mySQL 表格
id | departments_fk | qsps_fk | visible
270 | 1 | 218 | 1
271 | 22 | 218 | 0
272 | 1 | 219 | 0
273 | 22 | 219 | 1
274 | 1 | 220 | 1
275 | 22 | 220 | 1
HTML代码
<form action="edit-qsps.php?qspName=<?php echo $current_qsp["id"]; ?>" method="post">
<div class="qsp-name">QSP Name:
<input type="text" name="qsp_name" class="name-box" value="<?php echo $current_qsp["qsp_name"]; ?>" />
</div>
<div class="rev">QSP Rev:
<input type="text" name="qsp_rev" class="rev-box" value="<?php echo $current_qsp["qsp_rev"]; ?>" />
</div>
<div class="qsp-departments">Department:</div>
<?php
global $db_connection;
global $department_checked;
global $current_qsp;
global $id_department;
echo "<ul>";
$qsp_department_list = find_all_departments();
$department['id'] = $id_department;
while($department = mysqli_fetch_assoc($qsp_department_list)) {
echo htmlentities($department['department_name']);
echo " : ";
$qsp_department_checks = find_all_checks_for_department($current_qsp['id']);
$checks = $department_checked;
global $department_checked;
while($checks = mysqli_fetch_assoc($qsp_department_checks)) {
if ($current_qsp['id'] == $checks['qsps_fk'] && $checks['departments_fk'] == $department['id']) {
echo "<div class='department-checked'>";
echo "<input type='hidden' name='hidden-checkBoxes[]' value='";
echo htmlentities($checks['departments_fk']);
echo "' />";
echo "<input type='checkbox' name='department-checkBoxes[]' value='";
echo htmlentities($checks['departments_fk']);
echo "' ";
if($checks['checked'] == '1'){
echo "checked='checked'";
}
echo " />";
echo "</label>";
echo "</div>";
}
}
mysqli_free_result($qsp_department_checks);
}
mysqli_free_result($qsp_department_list);
echo "</ul>";
?>
<div class="create-btn">
<input type="submit" name="submit" value="UPDATE QSP" />
</div>
</form>
PHP 处理代码
<?php
if (isset($_POST['submit'])) {
global $db_connection;
global $id_department;
$id_qsp = $current_qsp["id"];
$qsp_name = mysql_prep($_POST["qsp_name"]);
$qsp_rev = mysql_prep($_POST["qsp_rev"]);
$query1 = "UPDATE qsps SET ";
$query1 .= "qsp_name = '{$qsp_name}', ";
$query1 .= "qsp_rev = '{$qsp_rev}' ";
$query1 .= "WHERE id = {$id_qsp} ";
$query1 .= "LIMIT 1";
$result1 = mysqli_query($db_connection, $query1);
foreach($_POST['hidden-checkBoxes'] as $checked) {
if (isset($_POST['department-checkBoxes'])) {
$val = 1;
} else {
$val = 0;
}
$query2 = "UPDATE junction_departments_qsps SET ";
$query2 .= "checked = {$val} ";
$query2 .= "WHERE departments_fk = {$checked} ";
$query2 .= "AND qsps_fk = {$id_qsp} ";
$result2 = mysqli_query($db_connection, $query2);
}
redirect_to("edit-qsps.php");
} else {
// Failure
$message = "Employee Update Failed.";
}
?>
更新了流程代码
if (isset($_POST['submit'])) {
global $db_connection;
global $id_department;
$id_qsp = $current_qsp["id"];
$qsp_name = mysql_prep($_POST["qsp_name"]);
$qsp_rev = mysql_prep($_POST["qsp_rev"]);
$query1 = "UPDATE qsps SET ";
$query1 .= "qsp_name = '{$qsp_name}', ";
$query1 .= "qsp_rev = '{$qsp_rev}' ";
$query1 .= "WHERE id = {$id_qsp} ";
$query1 .= "LIMIT 1";
$result1 = mysqli_query($db_connection, $query1);
if(!empty($_POST['department-checkBoxes'])) {
foreach($_POST['department-checkBoxes'] as $check) {
echo $check;
//only checked checkboxes come to $POST
$query2 = "UPDATE junction_departments_qsps SET ";
$query2 .= "checked = 1 ";
$query2 .= "WHERE departments_fk = {$check} ";
$query2 .= "AND qsps_fk = {$id_qsp} ";
$result2 = mysqli_query($db_connection, $query2);
}
}
redirect_to("edit-qsps.php");
} else {
// Failure
$message = "Employee Update Failed.";
}
您需要遍历 'department-checkBoxes'。现在您没有获得它们的值,只是检查是否设置了整个 POST 变量。
if(!empty($_POST['department-checkBoxes'])) {
foreach($_POST['department-checkBoxes'] as $check) {
echo $check;
//only checked checkboxes come to $POST
$query2 = "UPDATE junction_departments_qsps SET ";
$query2 .= "checked = 1 ";
$query2 .= "WHERE departments_fk = {$checked} ";
$query2 .= "AND qsps_fk = {$id_qsp} ";
$result2 = mysqli_query($db_connection, $query2);
}
表格
<form action="edit-qsps.php?qspName=<?php echo $current_qsp["id"]; ?>" method="post">
<div class="qsp-name">QSP Name:
<input type="text" name="qsp_name" class="name-box" value="<?php echo $current_qsp["qsp_name"]; ?>" />
</div>
<div class="rev">QSP Rev:
<input type="text" name="qsp_rev" class="rev-box" value="<?php echo $current_qsp["qsp_rev"]; ?>" />
</div>
<div class="qsp-departments">Department:</div>
<?php
global $db_connection;
global $current_qsp;
echo "<ul>";
$qsp_department_list = find_all_departments();
while($department = mysqli_fetch_assoc($qsp_department_list)) {
echo htmlentities($department['department_name']);
echo " : ";
$qsp_department_checks = find_all_checks_for_department($current_qsp['id']);
while($checks = mysqli_fetch_assoc($qsp_department_checks)) {
$id_checked = htmlentities($checks['id']);
if ($current_qsp['id'] == $checks['qsps_fk'] && $checks['departments_fk'] == $department['id']) {
echo "<div class='department-checked'>";
echo "<input type='checkbox' name='hidden-checkBoxes[]' value='";
echo htmlentities($checks['id']);
echo "' style='display:none' checked='checked' />";
echo "<input type='checkbox' name='department-checkBoxes[]' value='";
echo htmlentities($checks['id']);
echo "' ";
if($checks['checked'] == '1'){
echo "checked='checked'";
}
echo " />";
echo "</label>";
echo "</div>";
}
}
mysqli_free_result($qsp_department_checks);
}
mysqli_free_result($qsp_department_list);
echo "</ul>";
?>
<div class="employee-category">Employee Category:</div>
<?php echo edit_employee_category_checkBoxes(); ?>
<div class="create-btn">
<input type="submit" name="submit" value="UPDATE QSP" />
</div>
</form>
PHP 处理代码
<?php
if (isset($_POST['submit'])) {
global $db_connection;
$id_qsp = $current_qsp["id"];
$qsp_name = mysql_prep($_POST["qsp_name"]);
$qsp_rev = mysql_prep($_POST["qsp_rev"]);
$query1 = "UPDATE qsps SET ";
$query1 .= "qsp_name = '{$qsp_name}', ";
$query1 .= "qsp_rev = '{$qsp_rev}' ";
$query1 .= "WHERE id = {$id_qsp} ";
$query1 .= "LIMIT 1";
$result1 = mysqli_query($db_connection, $query1);
$departments_id = $_POST["hidden-checkBoxes"];
$hidden_depart = $_POST["department-checkBoxes"];
if(isset($_POST['hidden-checkBoxes'])) {
foreach ($departments_id as $depart_id){
$visible_check = in_array($depart_id, $hidden_depart) ? 1 : 0;
$query2 = "UPDATE junction_departments_qsps SET ";
$query2 .= "checked = {$visible_check} ";
$query2 .= "WHERE id = {$depart_id} ";
$result2 = mysqli_query($db_connection, $query2);
}
}
redirect_to("edit-qsps.php");
} else {
// Failure
$message = "Employee Update Failed.";
}
?>
我可以让复选框更新,但它们不能完全正常工作。当两个框都为空时,我可以选中其中一个,并且两个框都会更新为选中状态。如果两个框都被选中,我必须取消选中两个框才能将它们更新为未选中。我无法让一个框被选中而另一个框被取消选中。
有什么想法吗?
非常感谢您的帮助。
mySQL 表格
id | departments_fk | qsps_fk | visible
270 | 1 | 218 | 1
271 | 22 | 218 | 0
272 | 1 | 219 | 0
273 | 22 | 219 | 1
274 | 1 | 220 | 1
275 | 22 | 220 | 1
HTML代码
<form action="edit-qsps.php?qspName=<?php echo $current_qsp["id"]; ?>" method="post">
<div class="qsp-name">QSP Name:
<input type="text" name="qsp_name" class="name-box" value="<?php echo $current_qsp["qsp_name"]; ?>" />
</div>
<div class="rev">QSP Rev:
<input type="text" name="qsp_rev" class="rev-box" value="<?php echo $current_qsp["qsp_rev"]; ?>" />
</div>
<div class="qsp-departments">Department:</div>
<?php
global $db_connection;
global $department_checked;
global $current_qsp;
global $id_department;
echo "<ul>";
$qsp_department_list = find_all_departments();
$department['id'] = $id_department;
while($department = mysqli_fetch_assoc($qsp_department_list)) {
echo htmlentities($department['department_name']);
echo " : ";
$qsp_department_checks = find_all_checks_for_department($current_qsp['id']);
$checks = $department_checked;
global $department_checked;
while($checks = mysqli_fetch_assoc($qsp_department_checks)) {
if ($current_qsp['id'] == $checks['qsps_fk'] && $checks['departments_fk'] == $department['id']) {
echo "<div class='department-checked'>";
echo "<input type='hidden' name='hidden-checkBoxes[]' value='";
echo htmlentities($checks['departments_fk']);
echo "' />";
echo "<input type='checkbox' name='department-checkBoxes[]' value='";
echo htmlentities($checks['departments_fk']);
echo "' ";
if($checks['checked'] == '1'){
echo "checked='checked'";
}
echo " />";
echo "</label>";
echo "</div>";
}
}
mysqli_free_result($qsp_department_checks);
}
mysqli_free_result($qsp_department_list);
echo "</ul>";
?>
<div class="create-btn">
<input type="submit" name="submit" value="UPDATE QSP" />
</div>
</form>
PHP 处理代码
<?php
if (isset($_POST['submit'])) {
global $db_connection;
global $id_department;
$id_qsp = $current_qsp["id"];
$qsp_name = mysql_prep($_POST["qsp_name"]);
$qsp_rev = mysql_prep($_POST["qsp_rev"]);
$query1 = "UPDATE qsps SET ";
$query1 .= "qsp_name = '{$qsp_name}', ";
$query1 .= "qsp_rev = '{$qsp_rev}' ";
$query1 .= "WHERE id = {$id_qsp} ";
$query1 .= "LIMIT 1";
$result1 = mysqli_query($db_connection, $query1);
foreach($_POST['hidden-checkBoxes'] as $checked) {
if (isset($_POST['department-checkBoxes'])) {
$val = 1;
} else {
$val = 0;
}
$query2 = "UPDATE junction_departments_qsps SET ";
$query2 .= "checked = {$val} ";
$query2 .= "WHERE departments_fk = {$checked} ";
$query2 .= "AND qsps_fk = {$id_qsp} ";
$result2 = mysqli_query($db_connection, $query2);
}
redirect_to("edit-qsps.php");
} else {
// Failure
$message = "Employee Update Failed.";
}
?>
更新了流程代码
if (isset($_POST['submit'])) {
global $db_connection;
global $id_department;
$id_qsp = $current_qsp["id"];
$qsp_name = mysql_prep($_POST["qsp_name"]);
$qsp_rev = mysql_prep($_POST["qsp_rev"]);
$query1 = "UPDATE qsps SET ";
$query1 .= "qsp_name = '{$qsp_name}', ";
$query1 .= "qsp_rev = '{$qsp_rev}' ";
$query1 .= "WHERE id = {$id_qsp} ";
$query1 .= "LIMIT 1";
$result1 = mysqli_query($db_connection, $query1);
if(!empty($_POST['department-checkBoxes'])) {
foreach($_POST['department-checkBoxes'] as $check) {
echo $check;
//only checked checkboxes come to $POST
$query2 = "UPDATE junction_departments_qsps SET ";
$query2 .= "checked = 1 ";
$query2 .= "WHERE departments_fk = {$check} ";
$query2 .= "AND qsps_fk = {$id_qsp} ";
$result2 = mysqli_query($db_connection, $query2);
}
}
redirect_to("edit-qsps.php");
} else {
// Failure
$message = "Employee Update Failed.";
}
您需要遍历 'department-checkBoxes'。现在您没有获得它们的值,只是检查是否设置了整个 POST 变量。
if(!empty($_POST['department-checkBoxes'])) {
foreach($_POST['department-checkBoxes'] as $check) {
echo $check;
//only checked checkboxes come to $POST
$query2 = "UPDATE junction_departments_qsps SET ";
$query2 .= "checked = 1 ";
$query2 .= "WHERE departments_fk = {$checked} ";
$query2 .= "AND qsps_fk = {$id_qsp} ";
$result2 = mysqli_query($db_connection, $query2);
}
表格
<form action="edit-qsps.php?qspName=<?php echo $current_qsp["id"]; ?>" method="post">
<div class="qsp-name">QSP Name:
<input type="text" name="qsp_name" class="name-box" value="<?php echo $current_qsp["qsp_name"]; ?>" />
</div>
<div class="rev">QSP Rev:
<input type="text" name="qsp_rev" class="rev-box" value="<?php echo $current_qsp["qsp_rev"]; ?>" />
</div>
<div class="qsp-departments">Department:</div>
<?php
global $db_connection;
global $current_qsp;
echo "<ul>";
$qsp_department_list = find_all_departments();
while($department = mysqli_fetch_assoc($qsp_department_list)) {
echo htmlentities($department['department_name']);
echo " : ";
$qsp_department_checks = find_all_checks_for_department($current_qsp['id']);
while($checks = mysqli_fetch_assoc($qsp_department_checks)) {
$id_checked = htmlentities($checks['id']);
if ($current_qsp['id'] == $checks['qsps_fk'] && $checks['departments_fk'] == $department['id']) {
echo "<div class='department-checked'>";
echo "<input type='checkbox' name='hidden-checkBoxes[]' value='";
echo htmlentities($checks['id']);
echo "' style='display:none' checked='checked' />";
echo "<input type='checkbox' name='department-checkBoxes[]' value='";
echo htmlentities($checks['id']);
echo "' ";
if($checks['checked'] == '1'){
echo "checked='checked'";
}
echo " />";
echo "</label>";
echo "</div>";
}
}
mysqli_free_result($qsp_department_checks);
}
mysqli_free_result($qsp_department_list);
echo "</ul>";
?>
<div class="employee-category">Employee Category:</div>
<?php echo edit_employee_category_checkBoxes(); ?>
<div class="create-btn">
<input type="submit" name="submit" value="UPDATE QSP" />
</div>
</form>
PHP 处理代码
<?php
if (isset($_POST['submit'])) {
global $db_connection;
$id_qsp = $current_qsp["id"];
$qsp_name = mysql_prep($_POST["qsp_name"]);
$qsp_rev = mysql_prep($_POST["qsp_rev"]);
$query1 = "UPDATE qsps SET ";
$query1 .= "qsp_name = '{$qsp_name}', ";
$query1 .= "qsp_rev = '{$qsp_rev}' ";
$query1 .= "WHERE id = {$id_qsp} ";
$query1 .= "LIMIT 1";
$result1 = mysqli_query($db_connection, $query1);
$departments_id = $_POST["hidden-checkBoxes"];
$hidden_depart = $_POST["department-checkBoxes"];
if(isset($_POST['hidden-checkBoxes'])) {
foreach ($departments_id as $depart_id){
$visible_check = in_array($depart_id, $hidden_depart) ? 1 : 0;
$query2 = "UPDATE junction_departments_qsps SET ";
$query2 .= "checked = {$visible_check} ";
$query2 .= "WHERE id = {$depart_id} ";
$result2 = mysqli_query($db_connection, $query2);
}
}
redirect_to("edit-qsps.php");
} else {
// Failure
$message = "Employee Update Failed.";
}
?>