I have this code where I can add and later on update multiple checkbox. I can already add the value of the of checkbox into the database but I'm having problem when updating it. The value inserted into the database for checkbox is either 1 or 0.
Insert HTML code :
<tr>
<td align="center" width="150"><input type="hidden" name="leave_al" value="0" />
<input type="checkbox" name="leave_al" value="1">Annual Leave </td>
<td align="center" width="150"><input type='text' size='15' name='annual_days' id='annual_days'> day(s) </td>
</tr>
<tr>
<td align="center" width="150"><input type="hidden" name="leave_matl" value="0" />
<input type="checkbox" name="leave_matl" value="1">Maternity Leave </td>
<td align="center" width="150"><input type='text' size='15' name='maternity_days' id='maternity_days'> day(s) </td>
</tr>
Update HTML code :
<?php
if ($row2['leave_al'] == 1)
{
echo "<td><input type='checkbox' name='leave_al' value='1' checked> Annual Leave </td>";
}
else
{
echo "<td><input type='checkbox' name='leave_al' value='0'> Annual Leave </td>";
}
if ($row2['leave_matl'] == 1)
{
echo "<td><input type='checkbox' name='leave_matl' value='1' checked> Maternity Leave </td>";
}
else
{
echo "<td><input type='checkbox' name='leave_matl' value='0'> Maternity Leave </td>";
}
?>
Update process code :
if(isset($_POST['update']))
{
$leave_al = ($_POST['leave_al']=="1"?1:0);
$leave_matl = ($_POST['leave_matl']=="1"?1:0);
$query = "UPDATE leaves
SET leave_al = '".$leave_al."', leave_matl = '".$leave_matl."',
WHERE emp_id= '$id'";
}
Based on the coding above, when I checked the box, the value will be opposite. For example if I checked the box, the value will be 0 in the database. Something seems wrong about this code.
A lot of examples I've seen showed on how to update with array. If you could help me guide the correct way to update the checkbox and changed the 1 0 value. Thanks!
Aucun commentaire:
Enregistrer un commentaire