p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Beginning PHP (http://p2p.wrox.com/forumdisplay.php?f=95)
-   -   Store Checkbox values in MySQL Database (http://p2p.wrox.com/showthread.php?t=31196)

jkilgore July 5th, 2005 10:41 AM

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.



richard.york July 7th, 2005 03:43 PM

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


All times are GMT -4. The time now is 10:08 AM.

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