Skip to main content
Participant
Sep 9, 2009 6:24 PM UTC
Question

Inserting multiple checkbox values to a single mySQL field?

My client's site was previously setup where on the site's update page, there are a number of checkboxes.. and when the page is updated, their PHP update code would take whatever checkboxes were checked, and insert their relative default values into a single field in the mySQL table.. each separated by a comma. 

For examle, of there are checkboxes named 1 and 2 and 3 and 4.. and only 1 and 4 are checked, then in the mySQL table/field, you would see this inserted:

1,4

The Dreamweaver PHP Update just inserts one value per field it looks like.. is there a way to modify it's UPDATE behavior/function so that we can accomplish this same goal?

This topic is closed to new replies. Start a new post to keep the conversation going.

3 replies

Participating Frequently
Sep 10, 2009 6:27 PM UTC

Be aware that storing multiple values in a single field violates the rules of database normalization. If the data will be used in any type of analysis, you should not be storing it this way. If the data is simply saving user preferences or something similar, it's probably ok.

Translate
_adrianAuthor
Participant
Sep 10, 2009 8:36 PM UTC

I agree... unfortunately, I didn't build the original and this is what I'm forced to work with. [sigh]

Translate
David_Powers
Inspiring
Sep 10, 2009 2:21 PM UTC

When using a checkbox group or a multiple-choice list, you need to add an empty pair of square brackets after the name like this:

<input type="checkbox" name="options[]" id="options1" value="sunroof" />>

<input type="checkbox" name="options[]" id="options2" value="satnav" />

When the form is submitted, $_POST['options'] or $_GET['options'] contains an array of the selected values.

To convert the array into a comma-separated string, use implode().

implode(',', $_POST['options'])

Translate