468,765 Members | 979 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

Trouble with inserting Checkbox values

107 100+
I have six checkboxes as shown below:
Expand|Select|Wrap|Line Numbers
  1. <table>
  2. <tr>
  3. <td><input name="spec1" type="checkbox" value="0" tabindex="11" /><label id="label">Bridge Construction</label></td>
  4. </tr>
  5. <tr>
  6. <td><input name="spec2" type="checkbox" value="1" tabindex="12" /><label id="label">Building Construction</label></td>
  7. </tr>
  8. <tr>
  9. <td><input name="spec3" type="checkbox" value="2" tabindex="13" /><label id="label">Dam Construction</label></td>
  10. </tr>
  11. <tr>
  12. <td><input name="spec4" type="checkbox" value="3" tabindex="14" /><label id="label">Power & Telecommunication Works</label></td>
  13. </tr>
  14. <tr>
  15. <td><input name="spec5" type="checkbox" value="4" tabindex="15" /><label id="label">Road Construction</label></td>
  16. </tr>
  17. <tr>
  18. <td><input name="spec6" type="checkbox" value="5" tabindex="16" /><label id="label">Others</label></td>
  19. </tr>
  20. </table>
  21.  
And I have been trying to insert the checkbox values to my database in the following way.
Expand|Select|Wrap|Line Numbers
  1. if($_POST['spec1']==0){ $spec1="1";} else { $spec1="0"; }
  2. if($_POST['spec2']==1){ $spec2="1";} else { $spec2="0"; }
  3. if($_POST['spec3']==2){ $spec3="1";} else { $spec3="0"; }
  4. if($_POST['spec4']==3){ $spec4="1";} else { $spec4="0"; }
  5. if($_POST['spec5']==4){ $spec5="1";} else { $spec5="0"; }
  6. if($_POST['spec6']==5){ $spec6="1";} else { $spec6="0"; }
  7.  
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.
Feb 27 '09 #1
9 2946
Dormilich
8,651 Expert Mod 8TB
this is probably because NULL is equivalent to 0 in a simple boolean test. try following (value and type comparison):
Expand|Select|Wrap|Line Numbers
  1. $spec1 = ($_POST['spec1'] === 0) ? "1" : "0";
Feb 27 '09 #2
raamay
107 100+
Still it doesnt solve the problem. I have changed it in the following manner:
Expand|Select|Wrap|Line Numbers
  1. $spec1 = ($_POST['spec1'] === 0) ? "1" : "0";
  2. $spec2 = ($_POST['spec2'] === 1) ? "1" : "0";
  3. $spec3 = ($_POST['spec3'] === 2) ? "1" : "0";
  4. $spec4 = ($_POST['spec4'] === 3) ? "1" : "0";
  5. $spec5 = ($_POST['spec5'] === 4) ? "1" : "0";
  6. $spec6 = ($_POST['spec6'] === 5) ? "1" : "0";
  7.  
It gives me database error. I mean in my form if there is any error with the insert operation it returns me back the form that i submitted. That is what happening at present.

Plus if i check the first checkbox along with some others, the values are properly stored. Which means i have to check the first checkbox by any means. This output is available if i change it in the following way:
Expand|Select|Wrap|Line Numbers
  1. $spec1 = ($_POST['spec1'] === "0") ? 1 : 0;
  2. $spec2 = ($_POST['spec2'] === "1") ? 1 : 0;
  3. $spec3 = ($_POST['spec3'] === "2") ? 1 : 0;
  4. $spec4 = ($_POST['spec4'] === "3") ? 1 : 0;
  5. $spec5 = ($_POST['spec5'] === "4") ? 1 : 0;
  6. $spec6 = ($_POST['spec6'] === "5") ? 1 : 0;
  7.  
Feb 27 '09 #3
Dormilich
8,651 Expert Mod 8TB
what exactly is the database error?
Feb 27 '09 #4
raamay
107 100+
Database error is something like if some text value is inserted in a int field. There can be many of them. And by the way my db table has int(1) as the data type to store the value of checkboxes.
Feb 27 '09 #5
Dormilich
8,651 Expert Mod 8TB
then try to get the sql error message. like (depending on your code there may be different approaches to get the error message)
Expand|Select|Wrap|Line Numbers
  1. mysql_query($sql) or die(mysql_error());
  2.  
  3. // in case you use exceptions
  4. try {
  5. // db code
  6. }
  7. catch (Exception $e)
  8. {
  9.     echo $e->getMessage();
  10. }
otherwise guessing what the error might be is not very effective.
Feb 27 '09 #6
Markus
6,050 Expert 4TB
You're trying to insert a string type into an int type column. Correct this and you'll be OK.
Feb 27 '09 #7
devsusen
136 100+
Your code should be :
Expand|Select|Wrap|Line Numbers
  1. $spec1 = (isset($_POST['spec1'])) ? 1 : 0; 
  2. $spec2 = (isset($_POST['spec2'])) ? 1 : 0;  
  3. $spec3 = (isset($_POST['spec3'])) ? 1 : 0;  
  4. $spec4 = (isset($_POST['spec4'])) ? 1 : 0;  
  5. $spec5 = (isset($_POST['spec5'])) ? 1 : 0;  
  6. $spec6 = (isset($_POST['spec6'])) ? 1 : 0;  
  7.  
Feb 27 '09 #8
Dormilich
8,651 Expert Mod 8TB
@devsusen
note: line #2 is missing a closing parenthesis
Feb 27 '09 #9
raamay
107 100+
sorry guys, there was no mistake in the code. it was a silly mistake of my own which i have solved. Anyway thankyou and sorry for troubling you all.
Feb 28 '09 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Andre Ranieri | last post: by
1 post views Thread by J Talbot | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.