473,396 Members | 2,059 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,396 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>
  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 3313
Dormilich
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
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,658 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,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());
  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,658 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

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

Similar topics

1
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...
2
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...
1
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">...
7
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...
1
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";
18
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...
0
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...
2
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...
5
matheussousuke
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.