Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #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:

<tr>
      <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>
    </tr>

    <tr>
      <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>
    </tr>

    <tr>
      <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>
    </tr>

    <tr>
      <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>
    </tr>

    <tr>
      <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>
    </tr>

    <tr>
    <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>
    </tr>

    <tr>
      <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>

    </tr>

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.


  #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
Default

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.
service[licenced]
service[bonded]
service[property_management]
...etc.

Then something like this:

Code:
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

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design


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





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