Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 5th, 2005, 10:41 AM
Registered User
Join Date: Jul 2005
Location: , , USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Store Checkbox values in MySQL Database

I need to know how to take multiple selections from a series of checkboxes and put those values into MySQL. I'm not interested in the easiest way, but the most efficient. I've researched this for several days and I know about serialize and unserialize, but If I'm not mistaken the values stored will not be searchable. I need to make sure they can still be searched. My questions are these. What data type should I use in MySQL for these values (Tiny INT/ENUM)? Also, how should I set up the form to take those values and pass them to MySQL? Should I set it up so that each checkbox has a different name, set the value to 1 and checked. What code do I need to pass those values to MySQL. My current checkbox code is as follows:

      <td width="255"><span class="style2">
        <input type="checkbox" name="service[]" value="cleaning">CLEANING</span></input></td>
      <td width="254"><span class="style2">
    <input type="checkbox" name="service[]" value="linens">LINENS</span></input></td>

      <td><span class="style2">
        <input type="checkbox" name="service[]" value="lawn_maint">LAWN MAINTENANCE </span></input></td>
      <td><span class="style2">
        <input type="checkbox" name="service[]" value="windows">WINDOWS</span></input></td>

      <td><span class="style2">
        <input type="checkbox" name="service[]" value="pool_maint">POOL MAINTENANCE</span></input></td>
      <td><span class="style2">
        <input type="checkbox" name="service[]" value="minor_hr">MINOR HOME REPAIR </span></input></td>

      <td><span class="style2">
        <input type="checkbox" name="service[]" value="major_hr">MAJOR HOME REPAIR </span></input></td>
      <td><span class="style2">
        <input type="checkbox" name="service[]" value="carpet_cleaning">CARPET CLEANING </span></input></td>

      <td height="23"><span class="style2">
        <input type="checkbox" name="service[]" value="plumbing">PLUMBING</span></input></td>
      <td><span class="style2">
    <input type="checkbox" name="service[]" value="electrician">ELECTRICIAN</span></input></td>

    <td height="23"><span class="style2">
      <input type="checkbox" name="service[]" value="prop_management">PROPERTY MANAGEMENT</span></input></td>
      <td><span class="style2"></span></td>

      <td height="23"><span class="style2">
        <input type="checkbox" name="service[]" value="licensed">LICENSED</span></input></td>
      <td><span class="style2">
    <input type="checkbox" name="service[]" value="bonded">BONDED</span></input></td>


I should add I'm brand new to PHP/MySQL programming. I have little to no experience...this is my first project. Any help will be valued, and someday I will return the favor to a newbie.

Reply With Quote
  #2 (permalink)  
Old July 7th, 2005, 03:43 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts

I can only wonder how you're sorting through all those values.

I'd probably do a three field database table
[ id ] [ configuration/preference/directive ] [ value ]

In the form I'd do something other than service[] to keep track of all of the fields.

Then something like this:

foreach ($_POST['service'] as $directive => $value)
    mysql_query("INSERT INTO `service_vars` VALUES($id, '{$directive}', ". (int) $value .")");
Where $id is the user_id, or whatever other id your're attaching this information to.

I'd use a tinyint field for value, if nothing more than 0 || 1 will be the value.

Just my $0.02


Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to store checkbox value in database mona_1dec Struts 22 July 8th, 2011 04:29 PM
how to store 2 checkbox values in separate lines i jennypretty Classic ASP Basics 1 June 3rd, 2007 06:39 PM
How 2 store checkboxes values in MySQL babaloui Beginning PHP 0 March 13th, 2006 04:05 AM
How to insert checkbox values to mysql using php method PHP Databases 0 February 21st, 2006 10:47 PM
To split values and store in the database lily611 SQL Server 2000 3 July 15th, 2004 09:01 AM

All times are GMT -4. The time now is 01:19 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.