mercredi 14 janvier 2015

How to get my checkbox loop to update each checkbox individually instead of both of them in php and mySQL?

I can get the checkboxes to update but they aren’t working exactly right. When both boxes are empty I can check either one of them and both boxes get updated to checked. If both boxes are checked I have to uncheck both boxes for them to be updated to unchecked. I can’t get one box to be checked and the other to be unchecked.


Any ideas?


I really appreciate your help.


mySQL Tabel



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 Code



<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 Process Code



<?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.";
}

?>




Aucun commentaire:

Enregistrer un commentaire