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

multiple checkbox values

Guest
Jul 18, 2007 Jul 18, 2007
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!


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 ,
Jul 19, 2007 Jul 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/
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 ,
Jul 19, 2007 Jul 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
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
Guest
Jul 19, 2007 Jul 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?

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 ,
Jul 19, 2007 Jul 19, 2007
On Thu, 19 Jul 2007 16:39:35 +0000 (UTC), "jamawo"
<webforumsuser@macromedia.com> wrote:

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

Yep. Sorry I overlooked the naming part.


> Here is the code again for reference:
>
>GetSQLValueString(is_array($_POST['checkbox'])?join(',',$_POST['checkbox']):$_PO
>ST['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

GetSQLValueString is a Dreamweaver function that will format and add
quotes to string values to make them work correctly in an SQL statement.
The code I gave you does this:

if the $_POST['checkbox'] value passed by the form is an array, join the
array elements into a comma separated string.

If it's not an array, just use the value passed.

Once all that is done, the value (either original or array joined into a
string) is passed to the GetSQLValueString function.

Gary
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 ,
Jul 19, 2007 Jul 19, 2007
Gary White wrote:
>> GetSQLValueString(is_array($_POST['checkbox'])?join(',',$_POST['checkbox']):$_PO
>> ST['checkbox'],"text");,
>>
> GetSQLValueString is a Dreamweaver function that will format and add
> quotes to string values to make them work correctly in an SQL statement.
> The code I gave you does this:

There are two problems with this:

1. If no checkbox is selected, $_POST['checkbox'] won't be defined.
Depending on the level of error reporting in PHP, this is likely to
trigger an error message. It will also result in the SQL query
attempting to insert NULL into the checkbox column. Unless the column is
defined as accepting NULL values, this will prevent the query from
executing.

2. It edits the Dreamweaver server behavior code, which normally
prevents you from using the Server Behavior panel to reopen the Insert
Record dialog box. Even if it doesn't prevent you from doing this,
Dreamweaver is likely to rewrite this code when you click OK.

You can get round the first issue by amending the code like this:

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

This adds isset() to make sure that $_POST['checkbox'] actually exists
before you try to use it, and sets the value to an empty string if
$_POST['checkbox'] hasn't been defined.

However, this still has the disadvantage that you can't use the Insert
Record dialog box any more. My preference is to put the code that
formats $_POST['checkbox'] outside the server behavior. My original
suggestion was this:

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

However, on reflection, it needs to be this:

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

If you put this in a separate code block right at the top of the page,
the Dreamweaver server behavior remains fully editable in the Insert
Record dialog box, but you get the result that you want.

The difference between Gary's code and mine is that he uses the
conditional operator instead of an if... else statement, and join()
instead of implode(). The two functions are identical.

If you want to use the conditional operator, my version of the code
looks like this:

$_POST['checkbox'] = (isset($_POST['checkbox']) &&
is_array($_POST['checkbox'])) ? implode(',', $_POST['checkbox']) : '';

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
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 ,
Jul 19, 2007 Jul 19, 2007
LATEST
On Thu, 19 Jul 2007 19:05:44 +0100, David Powers <david@example.com>
wrote:

>1. If no checkbox is selected, $_POST['checkbox'] won't be defined.
>Depending on the level of error reporting in PHP, this is likely to
>trigger an error message. It will also result in the SQL query
>attempting to insert NULL into the checkbox column. Unless the column is
>defined as accepting NULL values, this will prevent the query from
>executing.

Actually two good points there. On my own servers, it wouldn't be a
problem, but there is the potential depending on the conditions you
mention regarding error reporting and field definition, David.

Thanks,

Gary
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
Guest
Jul 19, 2007 Jul 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.
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 ,
Jul 19, 2007 Jul 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/
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