473,772 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trouble with inserting Checkbox values

107 New Member
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 3334
Dormilich
8,658 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
what exactly is the database error?
Feb 27 '09 #4
raamay
107 New Member
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 Recognized Expert Moderator Expert
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 Recognized Expert Expert
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 New Member
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 Recognized Expert Moderator Expert
@devsusen
note: line #2 is missing a closing parenthesis
Feb 27 '09 #9
raamay
107 New Member
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
13296
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 the application's front end the file appears to be corrupt. The front-end application has a way of inserting files to the column, however when I analyze this in SQL Profiler the contents of the Byte array appear to be quite different than the one...
2
4722
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 question. I have a web survey that I am working on and have successfully dynamically taken the info from a database, displayed it on the screen and then taken the users answers and inserted them into a
1
2206
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"> <input type="checkbox" name="checkbox" value="2ndValue"> <input type="checkbox" name="checkbox" value="3rdValue">
7
2427
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 inserting except checkbox value, i dont know in sql server 2005 which data type i have to use for that column to insert the checkbox checked values please reply me its very urgent
1
1385
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
4049
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 new to this stuff and have tried couple times but no luck. Sorry to waste your time again, but would appreciate if you can help me out. I have a form that has a checkbox name of ExpeditedRequest. I can't send the unchecked or checked value to the...
0
1071
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 event......Need help.... <asp:GridView ID="GridView2" DataSourceID="SqlDataSource2" DataKeyNames="AlertID,ItemID" AutoGenerateColumns="False" runat="server" Width="839px" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True"> ...
2
3096
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 section: Discography --------------------- DiscID
5
13379
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 http://mghospedagem.com/images/controlpanel.jpg instead of http://mghospedagem.comhttp://mghospedagem.com/images/controlpanel.jpg As u see, there's the website URL before the image URL.
0
9619
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8934
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.