473,513 Members | 3,949 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Checkbox forms, php & mysql

Okay, I'm about to add some checkboxes to a form, and then store the data in
a column on a MySQL database. Just wanted to know best practice, at the
moment I'm writing and thinking .. God that's ungly code ...

<input type="checkbox" name="delta" <?php if ($delta==1) { echo
"checked"; }?>> Delta<br>
<input type="checkbox" name="mastercard" <?php if ($mastercard==1) {
echo "checked"; }?>> Mastercard<br>
<input type="checkbox" name="solo" <?php if ($solo==1) { echo
"checked"; }?>> Solo<br>
<input type="checkbox" name="switch" <?php if ($switch==1) { echo
"checked"; }?>> Switch<br>
<input type="checkbox" name="visa" <?php if ($visa==1) { echo
"checked"; }?>> Visa<br>

MySQL database fields ...
delta binary(1) No 0
mastercard binary(1) No 0
solo binary(1) No 0
switch binary(1) No 0
visa binary(1) No 0

When I output the fields, I get "on" or empty, and am wondering whether I am
making a good decision to do the code this way. Now I need to figure out how
to update the database based on the forms output.

Thanks
May 27 '06 #1
3 11826
Rik
elyob wrote:
Okay, I'm about to add some checkboxes to a form, and then store the
data in a column on a MySQL database. Just wanted to know best
practice, at the moment I'm writing and thinking .. God that's ungly
code ...

<input type="checkbox" name="delta" <?php if ($delta==1) { echo
"checked"; }?>> Delta<br>
<input type="checkbox" name="mastercard" <?php if
($mastercard==1) { echo "checked"; }?>> Mastercard<br>
<input type="checkbox" name="solo" <?php if ($solo==1) { echo
"checked"; }?>> Solo<br>
<input type="checkbox" name="switch" <?php if ($switch==1) { echo
"checked"; }?>> Switch<br>
<input type="checkbox" name="visa" <?php if ($visa==1) { echo
"checked"; }?>> Visa<br>

MySQL database fields ...
delta binary(1) No 0
mastercard binary(1) No 0
solo binary(1) No 0
switch binary(1) No 0
visa binary(1) No 0

When I output the fields, I get "on" or empty, and am wondering
whether I am making a good decision to do the code this way. Now I
need to figure out how to update the database based on the forms
output.


On the right track IMO.

You could query the database like this (Warning:I'm an xhtml-fetishist):

SELECT `id`, `some_field`, IF(`delta` = 1, ' checked="checked"','') AS
'delta', etc.....

And, assuming you have a $row array resulting form the query:

echo "<input type=\"checkbox\" name="delta"{$row['delta']} />";

On submitting the update:
$delta = (isset($_POST['delta']))?'1':'0';

"UPDATE table SET delta='$delta',ect...."

Grtz,
--
Rik Wasmus
May 27 '06 #2
elyob wrote:
Okay, I'm about to add some checkboxes to a form, and then store the data in
a column on a MySQL database. Just wanted to know best practice, at the
moment I'm writing and thinking .. God that's ungly code ...

<input type="checkbox" name="delta" <?php if ($delta==1) { echo
"checked"; }?>> Delta<br>
<input type="checkbox" name="mastercard" <?php if ($mastercard==1) {
echo "checked"; }?>> Mastercard<br>
<input type="checkbox" name="solo" <?php if ($solo==1) { echo
"checked"; }?>> Solo<br>
<input type="checkbox" name="switch" <?php if ($switch==1) { echo
"checked"; }?>> Switch<br>
<input type="checkbox" name="visa" <?php if ($visa==1) { echo
"checked"; }?>> Visa<br>

MySQL database fields ...
delta binary(1) No 0
mastercard binary(1) No 0
solo binary(1) No 0
switch binary(1) No 0
visa binary(1) No 0

When I output the fields, I get "on" or empty, and am wondering whether I am
making a good decision to do the code this way. Now I need to figure out how
to update the database based on the forms output.

Thanks


Are these mutually exclusive? Or can they have mastercard and visa checked at
the same time?

If they're mutually exclusive, a better way is to use a single field to hold a
value (perhaps an ENUM). Otherwise - what i f due to some problem you have both
mastercard and visa on in the field?
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 27 '06 #3
Another option is to loose the binary and use a text field and just
store the on or blank. It really just depends on what you are going
for.

Using int you would do it such:

$d = null;
if ($_POST['check_box_name']=="on") {
$d = 1;
} else {
$d = 0;
}

Then just update your database accordingly.

May 28 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4670
by: RelaxoRy | last post by:
Reposted (not sure if got sent) I have results being displayed each with its own checkbox name "selected" and value "id". When someone checks a checkbox, and then continues to page through the data (mysql select), I want that checkbox to be remembered. Basically I want the user to have multiple selections of checkboxes when paging...
1
9457
by: kiran | last post by:
I cratee a form to meet the fallowing requirement. 1. If any node is selected the corresponding checkbox should be checked 2. If any checkbox is clicked the corresponding node should be selected 1st requirement is working ,but, 2nd requirement is not wokring properly. when I click a checkbox the coresponding node is being selected , but...
29
3111
by: Amer Neely | last post by:
I've got a dynamically built form with checkboxes for each element ( a list of file names in a directory). I need to grab only those checkboxes that are checked, so I can then delete those files. Does each checkbox name have to be unique? I was hoping to just group them under one name, and select from that array. PHP is not my native...
9
1785
by: morellik | last post by:
Dear all, I have a program that creates dinamically a web page. In the page I have the following function to check how many checkbox are checked. function tarInfo(info) { var i=0; var c=0; var l=document.forms.selected_files.length; alert(l);
3
3667
by: uremog | last post by:
I have a set of of check boxes. onClick, the checkboxes call check_radio and recup_checkbox. the referenced radios function as group selectors. check_radio just unchecks the radios if someone manually selects checkboxes. i think the problem is in the add_selection and del_selection as suggested by the alerts, which are for debugging only. ...
1
1620
by: inamul | last post by:
I want to select CheckBox based on data retrieved from mysql database table. Could anyone show me the simple way of doing it. Data store in table under colum "sectionOfInterest" is shown below ------------------------------------------------------------------------ "Commercial Security Middle East, Homeland Security and Policing Middle...
1
1777
by: inamul | last post by:
I want to select CheckBox based on data retrieved from mysql database table. Could anyone show me the simple way of doing it. Data store in table under colum "sectionOfInterest" is shown below ------------------------------------------------------------------------ "Commercial Security Middle East, Homeland Security and Policing Middle...
0
3091
by: TechnoAtif | last post by:
<?php include "dbconnect.php"; include "commonFunc.php"; ?> <!----------------------------------> <table width="80%" border="1" cellpadding="2" cellspacing="0"> <tr > <td colspan="2"><p>
6
1963
by: thenagus | last post by:
I am working on a youth rights website, and while working on the membership application, I receive an datatype mismatch error. I have pinpointed the error to be related to checkbox values. All form variables are passed to the next page which then stores the forms values as session variables. In the database, each of the database fields...
0
7270
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...
0
7397
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. ...
0
7563
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7125
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...
0
5703
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...
1
5102
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4757
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3252
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...
0
470
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...

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.