mardi 23 décembre 2014

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

OK so, I'm trying to insert some data into mySQL using text fields and checkboxes. I have the text fields going to one table and the checkboxes going to another table. I can get data to insert into both table, but I'm trying to get my second checkbox to insert data into the visible column based on which department is selected. My thought is to hide the department-checkBoxes[] and show the visible-checkBoxes[] as long as I can get the visible-checkBoxes[] data to insert.


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


Here is my form.



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


Here is my PHP for department-checkBoxes[]



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


Here is my PHP for visible-checkBoxes[]



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


Thank you I appreciate the Help!





Aucun commentaire:

Enregistrer un commentaire