I have a list of items that will be fetched from DB. Each row have one checkbox and an associated value in DB. If it is checked and saved the value changes to 1 else it will be 0.
So on page load there is a drop down to select the items.Based on the selection, the data will be fetched. Values with 1 will be shown checked and 0 will be unchecked. I am trying to update this to DB on button click. So newly checked ones will be saved with status 1 and unchecked will be updated with status 0.
If one value was previously 1 and is unchecked during update, the value should change to 0.
My code:
HTML:
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<tbody>
<tr class="even pointer" id="<?php echo $row['tcode'] ?>">
<td class="a-center ">
<?php if($row['selected'] == 0){ ?>
<input class="flat" type="checkbox" name="check_list[]" value="<?php echo $row['tcode'] ?>">
<?php } else{ ?>
<input class="flat" type="checkbox" name="check_list[]" value="<?php echo $row['tcode'] ?>" checked>
<?php } ?>
</td>
<td class=" "><?php echo $row['task_name'] ?></td>
<td class=" "><?php echo $row['tcode'] ?></td>
<td class=" "><?php echo $row['task_category'] ?></td>
</td>
</tr>
</tbody>
<?php } ?>
PHP:
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
$projectcode=$_POST['projectcategory'];
// Loop to store and display values of individual checked checkbox.
echo $_POST['check_list'];
foreach($_POST['check_list'] as $selected){
$sql2="UPDATE mappedtask SET selected=1 WHERE pcode = '$projectcode' AND pcode = '.$_POST[tcode].' ";
$result2=$conn->query($sql2);
}
}
}
Aucun commentaire:
Enregistrer un commentaire