Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Inserting multiple checkbox values to a single mySQL field?

New Here ,
Sep 09, 2009 Sep 09, 2009

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?

TOPICS
Server side applications
3.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 10, 2009 Sep 10, 2009

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 10, 2009 Sep 10, 2009

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 10, 2009 Sep 10, 2009
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines