473,231 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

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>
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"; }
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 3305
8,658 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
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";
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;
Feb 27 '09 #3
8,658 Expert Mod 8TB
what exactly is the database error?
Feb 27 '09 #4
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
8,658 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());
  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
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
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;  
Feb 27 '09 #8
8,658 Expert Mod 8TB
note: line #2 is missing a closing parenthesis
Feb 27 '09 #9
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

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

Similar topics

by: Andre Ranieri | last post by:
I'm having trouble programatically inserting an Excel file into an Image column in our CRM package's SQL 2000 database. The function appears to work ok, but when I attempt to access the file through...
by: clinttoris | last post by:
Hello, If someone could help me it would be appreciated as I am not having much luck. I'm struggling with my asp code and have some questions relating to asp and oracle database. First...
by: J Talbot | last post by:
Hi Was wondering if anyone could help me with this problem : If I have three checkboxes with different values on a form like : <input type="checkbox" name="checkbox" value="1stValue">...
by: bhargavigupta | last post by:
hi, In my application i have one checkbox when ever i am entering values to the page it has to store the values into the database table using insert statement , remaining values r...
by: arial | last post by:
I am having problem inserting checkbox value into database table. My database field is of type char(1). and my code is like this: string cc= null; if (chkdiscipline.checked) { cc = "T";
by: hotflash | last post by:
Hi Mark et All, I understand that you created a very professional document and a similar issue forum out there regarding to Inserting the checkbox value into MS Access using ASP however; I am so...
by: rajesh0303 | last post by:
Iam get null values in the datakeynames AlertID,ItemID while executing the Insert Command....I am Inserting using Externel button Event.Here is the code of Gridiew ,Its datasource and the button...
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.