473,379 Members | 1,386 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,379 software developers and data experts.

Save Checkbox Value (1 or 0) With mySQL

233 100+
I have a form with several checkboxes that I would like to save a bit value in the corresponding column in mySQL. As far as the html is concerned, I have each checkboxs' value set to '1'. I have created variables to capture the POST value for each html field, and then I use that variable in a SQL insert statement.

Expand|Select|Wrap|Line Numbers
  1. if(isset($_POST['action']) && $_POST['action'] == 'submitform')
  2. {
  3.     $personalizedname = $_POST['personalizedname'];
  4.     $personalizedaddress = $_POST['personalizedaddress'];
  5.     $personalizedphone= $_POST['personalizedphone'];
  6.     $mountain = $_POST['mountain'];
  7.     $surfboards = $_POST['surfboards'];
  8.     $bird = $_POST['bird'];
  9.     $sailboats = $_POST['sailboats'];
  10.     $beach = $_POST['beach'];
  11.     $name = $_POST['Name'];
  12.     $company = $_POST['Company'];
  13.     $address1 = $_POST['address1'];
  14.     $address2 = $_POST['address2'];
  15.     $citystatezip = $_POST['citystatezip'];
  16.     $phone = $_POST['phone'];
  17.     $email = $_POST['email'];
  18.     $comments = $_POST['comments'];
  19.     //$type = $_POST['type'];
  20.     $mailingbrochureoffer = $_POST['mailingbrochureoffer'];
  21.     $emailoffer = $_POST['emailoffer'];
  22.     $tradeshowoffer = $_POST['tradeshowoffer'];
  23.     $otheroffer = $_POST['otheroffer'];
  24.         $othertext = $_POST['othertext'];
  25.     $couponcode = $_POST['couponcode'];
  26. }
Expand|Select|Wrap|Line Numbers
  1. $insert_query = sprintf("INSERT INTO BagTags (personalizedname, personalizedaddress, personalizedphone, mountain, surfboards, bird, sailboats, beach, name, company, address1, address2, citystatezip, phone, email, asippaiupicid, comments, mailingbrochureoffer, emailoffer, tradeshowoffer, otheroffer, othertext, couponcode) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 'CompanyA', %s, %s, %s, %s, %s, %s, %s)",
  2.     sanitize($personalizedname, "text"),
  3.     sanitize($personalizedaddress, "text"),
  4.     sanitize($personalizedphone, "int"),
  5.     $mountain,
  6.     $surfboards,
  7.     $bird,
  8.     $sailboats,
  9.     $beach,
  10.     sanitize($name, "text"),
  11.     sanitize($company, "text"),
  12.     sanitize($address1, "text"),
  13.     sanitize($address2, "text"),
  14.     sanitize($citystatezip, "text"),
  15.     sanitize($phone, "int"),
  16.     sanitize($email, "text"),
  17.     sanitize($comments, "text"),
  18.     //sanitize($type, "text"),
  19.     $mailingbrochureoffer,
  20.     $emailoffer,
  21.     $tradeshowoffer,
  22.     $otheroffer,
  23.     sanitize($othertext, "text"),
  24.     $couponcode);
On the database side, the columns where the checkboxes will be saved are currently varchar(32) with a default value of '0'. I plan to change them back to a bit value once I have things working correctly. Currently, I receive a SQL error if any of the checkboxes are unchecked. If all of the checkboxes are checked, the value in the checkbox columns does not equal '1' as I would have expected.
Jun 2 '11 #1
4 11039
code green
1,726 Expert 1GB
Unchecked check boxes are not set as a POST[] element.
So you need something like
Expand|Select|Wrap|Line Numbers
  1. if(isset($_POST['checkbox']) { 
  2.  $var = 1; 
  3. } else { 
  4.  $var = 0; 
You are blindly placing a lot of form data into a DB with no validation,
not only is this bad practice, but it leads to problems such as yours
Jun 3 '11 #2
mcfly1204
233 100+
I realize now that there is no value returned for unchecked checkboxes. Thanks for the assumption in the last tip chief.
Jun 3 '11 #3
s4mm77
1
I might be missing some vital point here but...

Expand|Select|Wrap|Line Numbers
  1. $var = isset($_POST['checkbox']);
I mean, you wouldn't even need the whole $var, just surround it with isset()? It's what i've been doing ._.
Oct 26 '12 #4
viirre
1
IF you have an automated insert/update procedure, it's pretty easy to just output a hidden input with value = 0 right before the checkbox:
<input type="hidden" name="cb" value="0" />
<input type="checkbox" name="cb" value="1" />

Now you'll allways either get 0 or 1 from the POST
Nov 11 '12 #5

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

Similar topics

4
by: Jack | last post by:
Hi, I have a checkbox the value which goes to a database via a asp page that builds the sql string. In the front end asp page, the checkbox code is written as follows: <i><input...
1
by: irene | last post by:
Hi, I am trying to update the checkbox value in a datagrid. I am using TemplateColumn to add the checkbox to the datagrid and it is showing correct data from the database now. Ideally, the end user...
3
by: Julius Fenata | last post by:
Dear all, I have checkbox on my webform, and I want that when I click my checkbox, it should change status between true or false... Here is my code, // Code Behind CheckBox1.Attributes =...
1
by: Art | last post by:
Hi, I have a problem with checkbox value on the page connected to MS Access. Ms Access field is: Independent - Data Type: Yes/No My code is: <form name="form" method="post"...
3
by: shobu | last post by:
passing array checkbox value and update the database <?include 'dbconnect.php'; error_reporting(0);$update_qr="update...
1
by: Ashu Saxena | last post by:
Hi ! I'm a php developer. I'm developing a site into which I have to upload a text file and a image file from users through a browse box and then I have to retrieve that file and save it into...
11
by: manjunath1985 | last post by:
Hi, I am really having problem in checkbox.... i have a php page which displays the username retrieved from the database i want to create a checkbox for each username and pass the selected...
2
by: palanidharma | last post by:
hi, i writing the code for selectd check box value deleted.but the code not working <?php include("conn.php"); $sel= mysql_select_db("sangoma"); //include("function.php"); ?>
2
semanticnotion
by: semanticnotion | last post by:
I have 5 checkboxes in HTML page and i want to save the value of checked checkbox into mysql using PHP but when i save the form in database the value of checkbox is 1 while the value of checkbox is...
5
by: ndeeley | last post by:
Hi, This is driving me mad - can someone guide me in the right direction please? I am outputting the results of a query to a webpage - in this case drawings in a database. The user selects the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...

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.