I have six checkboxes as shown below:
And I have been trying to insert the checkbox values to my database in the following way.
Everything goes fine except that even if i dont check the first checkbox, the column corresponding to this checkbox in the database gets a value of 1. To be clear, if i check spec4 and spec5 the result stored in the database is fine as the corresponding column for this two checkbox is valued with 1. But along with it the value for spec1 is also added although i didn't check the first checkbox.
Any help in this situation could be a great help! Kindly correct my code if necessary or kindly help me how to insert multiple checkbox values in different fields of a table.
Code:
<table> <tr> <td><input name="spec1" type="checkbox" value="0" tabindex="11" /><label id="label">Bridge Construction</label></td> </tr> <tr> <td><input name="spec2" type="checkbox" value="1" tabindex="12" /><label id="label">Building Construction</label></td> </tr> <tr> <td><input name="spec3" type="checkbox" value="2" tabindex="13" /><label id="label">Dam Construction</label></td> </tr> <tr> <td><input name="spec4" type="checkbox" value="3" tabindex="14" /><label id="label">Power & Telecommunication Works</label></td> </tr> <tr> <td><input name="spec5" type="checkbox" value="4" tabindex="15" /><label id="label">Road Construction</label></td> </tr> <tr> <td><input name="spec6" type="checkbox" value="5" tabindex="16" /><label id="label">Others</label></td> </tr> </table>
Code:
if($_POST['spec1']==0){ $spec1="1";} else { $spec1="0"; }
if($_POST['spec2']==1){ $spec2="1";} else { $spec2="0"; }
if($_POST['spec3']==2){ $spec3="1";} else { $spec3="0"; }
if($_POST['spec4']==3){ $spec4="1";} else { $spec4="0"; }
if($_POST['spec5']==4){ $spec5="1";} else { $spec5="0"; }
if($_POST['spec6']==5){ $spec6="1";} else { $spec6="0"; }
Any help in this situation could be a great help! Kindly correct my code if necessary or kindly help me how to insert multiple checkbox values in different fields of a table.
Comment