Skip to main content
July 19, 2007
Question

multiple checkbox values

  • July 19, 2007
  • 4 replies
  • 3661 views
Using PHP/MySQL and Dreamweaver 8

Problem:
Insert multiple checkbox values into a single database field using Dreamweaver Server Behaviors

I know that the checkboxes must be turned into an array.

My form has this:
(other fields omitted for brevity)
<input name="checkbox" type="checkbox" id="checkbox[0]" value="apples" />apples
<input name="checkbox" type="checkbox" id="checkbox[1]" value="oranges" />oranges
<input name="checkbox" type="checkbox" id="checkbox[2]" value="pears" />pear

Dreamweaver produces this code (before I added more than one checkbox in the form)

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "testform")) {
$insertSQL = sprintf("INSERT INTO test (text, radiogroup, checkbox, username) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['text'], "text"),
GetSQLValueString($_POST['RadioGroup'], "text"),
GetSQLValueString($_POST['checkbox'], "text"),
GetSQLValueString($_POST['hiddenField'], "text"));

mysql_select_db($database_db, $db);
$Result1 = mysql_query($insertSQL, $db) or die(mysql_error());
}

How do I change this code so that the "GetSQLValueString" posts the checkbox as an array and inserts it into one field deliminated with commas?

I'm just learning PHP and know enough to be dangerous! :) Thanks!


This topic has been closed for replies.

4 replies

Inspiring
July 19, 2007
jamawo wrote:
> Wow, David... that is a well thought out explaination and I agree with you.
> After playing with this page for hours, the server behavior is indeed broken.
> I'm going to try to use your alternative suggestions now and put the code
> outside of the server behavior.

One of the main problems of using Dreamweaver server behaviors without a
solid grounding in PHP (or the server-side language of your choice) is
knowing how to tweak the code to fit your own requirements. I find DW
automatic code generation very convenient and time saving, but you
really do need an understanding of what's being created on your behalf
to get the best out of it.

> Ya know what? I wish I knew more PHP because I am just tempted to ditch
> letting Dreamweaver write the code. But I've got so many loose ends in my
> knowledge base that my head explodes. Problem is also, the more you mix with
> hand coding and Dreamweaver code, the more prone to errors your application
> becomes. The 2 just don't seem to mix well, especially when you are a noob
> coder.

I can understand your frustration.

<shameless plug>You might find it helpful to grab hold of a copy of my
"Foundation PHP for Dreamweaver 8", or if you're using CS3, "The
Essential Guide to Dreamweaver CS3 with CSS, Ajax, and PHP" (due out on
Monday). The approach I take in my books is to try to help you
understand the code, rather than simply telling you to point and click a
series of menu items.</shameless plug>

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
July 19, 2007
Wow, David... that is a well thought out explaination and I agree with you. After playing with this page for hours, the server behavior is indeed broken. I'm going to try to use your alternative suggestions now and put the code outside of the server behavior.

Ya know what? I wish I knew more PHP because I am just tempted to ditch letting Dreamweaver write the code. But I've got so many loose ends in my knowledge base that my head explodes. Problem is also, the more you mix with hand coding and Dreamweaver code, the more prone to errors your application becomes. The 2 just don't seem to mix well, especially when you are a noob coder.
Inspiring
July 19, 2007
On Thu, 19 Jul 2007 05:01:37 +0000 (UTC), "jamawo"
<webforumsuser@macromedia.com> wrote:

> GetSQLValueString($_POST['checkbox'], "text"),

Try changing the above line to this:

GetSQLValueString(is_array($_POST['checkbox'])?join(',',$_POST['checkbox']):$_POST['checkbox'],"text");,

Gary
July 19, 2007
David,
Thank you for clearing up the name attributes array. Your links also gave me additional insight into SET. I found this link was useful as well:
http://www.vbmysql.com/articles/mysql/the-mysql-set-datatype/

Gary,
You sir, resolved my question. After following David's suggestions for the naming attributes for the arrary, I changed my code to your statement, and voila... it works fantastic. This simple line of code educated me beyond just a copy/paste routine. I do have a question however.

Here is the code again for reference:
GetSQLValueString(is_array($_POST['checkbox'])?join(',',$_POST['checkbox']):$_POST['checkbox'],"text");,

While I am learning PHP, I try to read the PHP code in simple english which helps me to understand what is going on with the syntax. So, this is how I am reading this statement, broken down:
GetSQLValueString reads get the value of the string
(is_array reads this string is an array
($_POST['checkbox']) reads the field checkbox in the form with the Post method
?join reads how many to join together
(',',$_POST['checkbox']):$_POST['checkbox'], reads the fields checkbox 0, checkbox 1, checkbox 2
"text");, insert the data as text

Am I reading this statement correctly?

Inspiring
July 19, 2007
jamawo wrote:
> I know that the checkboxes must be turned into an array.

Correct, but it's the name attributes that need to be an array, not the
IDs. Your checkboxes should look like this:

<input name="checkbox[]" type="checkbox" id="apples" value="apples"
/>apples
<input name="checkbox[]" type="checkbox" id="oranges" value="oranges"
/>oranges
<input name="checkbox[]" type="checkbox" id="pears" value="pears" />pear

Note that the square brackets after the name attribute do not contain a
number. This is because the $_POST array includes only those checkboxes
that have been selected. If no checkboxes are selected,
$_POST['checkbox'] will not be defined.

> How do I change this code so that the "GetSQLValueString" posts the checkbox
> as an array and inserts it into one field deliminated with commas?
>
> I'm just learning PHP and know enough to be dangerous! :) Thanks!

So it seems. ;-)

Inserting the values into a single field is not necessarily the best way
to approach it unless you define the checkbox field to use the SET
column type in MySQL. This allows you to establish a predefined list of
acceptable values:

http://dev.mysql.com/doc/refman/5.0/en/set.html

Assuming that you have defined the column to use SET, the way to insert
the checked values into MySQL is by placing the following code above the
Insert Record server behavior code:

if (isset($_POST['checkbox']) && is_array($_POST['checkbox'])) {
$_POST['checkbox'] = implode(',', $_POST['checkbox']);
}

In other words, you convert the array to a comma-separated string with
implode():

http://www.php.net/manual/en/function.implode.php

Dreamweaver handles the rest.

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/