By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,856 Members | 2,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,856 IT Pros & Developers. It's quick & easy.

storing checkbox data using implode

P: 6
All of my other form data is stored correctly in the db except for my checkbox data. This column in my table is empty.

I have this checkbox group on my form:

<input name="cbItems[]" type="checkbox" id="cbItems" value="Item1">Item1
<input name="cbItems[]" type="checkbox" id="cbItems" value="Item2">Item2
<input name="cbItems[]" type="checkbox" id="cbItems" value="Item3">Item3
<input name="cbItems[]" type="checkbox" id="cbItems" value="Item4">Item4
<input name="cbItems[]" type="checkbox" id="cbItems" value="Item5">Item5
<input name="cbItems[]" type="checkbox" id="cbItems" value="Item6">Item6
<input name="cbItems[]" type="checkbox" id="cbItems" value="Item7">Item7
<input name="cbItems[]" type="checkbox" id="cbItems" value="Item8">Item8

This, among other things, is passed to a confirmation page where I have this:
---------------------------------------
<?php
if(!isset($_POST['cbItems']))
{
//array is empty
$_POST['cbItems'] = array();
echo "None selected.";
}
else
{
//must do this for checkboxes (passing multiple values)
foreach ($_POST['cbItems'] as $items)
{
echo " - $items";
}
}
?>
<input type='hidden' name='cbItems' value='<?php echo $_POST['$items'];?>'>
---------------------------------------


On the next page (last page) I connect to the db and insert into the table:
---------------------------------------
$cbItems = array($_POST['items']);
$cbItems_array = implode(",", $cbItems);

include 'db_connect.php';

$query_insert = "INSERT INTO healthed_workshop (date, fname, lname, phone, email, org, dlSubject,
other, dlMonth, dlDay, dlYear, dlTime1, dlTime2, location, attend, cbItems, taAdd)".
"VALUES (now(),'".$_POST['fname']."', '".$_POST['lname']."', '".$_POST['phone']."', '".$_POST['email']."',
'".$_POST['org']."', '".$_POST['dlSubject']."', '".$_POST['other']."', '".$_POST['dlMonth']."',
'".$_POST['dlDay']."', '".$_POST['dlYear']."', '".$_POST['dlTime1']."', '".$_POST['dlTime2']."',
'".$_POST['location']."', '".$_POST['attend']."', '$cbItems_array', '".$_POST['taAdd']."')";

echo "$query_insert";

mysql_query($query_insert) or die(mysql_error());

mysql_close;
?>
---------------------------------------


Am I missing something glaringly obvious? Thanks in advance!
Jul 27 '06 #1
Share this Question
Share on Google+
5 Replies


Banfa
Expert Mod 5K+
P: 8,916
Shouldn't

$cbItems_array = implode(",", $cbItems);

be

$cbItems_array = explode(",", $cbItems);

or alternitively just use $cbItems in the SQL statment instead of $cbItems_array

In your final page $_POST['items'] is no longer an array (I believe).

When debugging a quick

echo("<pre>\n");
print_r($_POST);
echo("</pre>\n");

is always handy.
Jul 27 '06 #2

P: 6
Shouldn't
$cbItems_array = implode(",", $cbItems);
be
$cbItems_array = explode(",", $cbItems);
Doing this just inserts the word "Array":

$cbItems = array($_POST['items']);
$cbItems_array = explode(",", $cbItems);

include 'db_connect.php';

$query_insert = "INSERT INTO healthed_workshop (date, fname, lname, phone, email, org, dlSubject,
other, dlMonth, dlDay, dlYear, dlTime1, dlTime2, location, attend, cbItems, taAdd)".
"VALUES (now(),'".$_POST['fname']."', '".$_POST['lname']."', '".$_POST['phone']."', '".$_POST['email']."',
'".$_POST['org']."', '".$_POST['dlSubject']."', '".$_POST['other']."', '".$_POST['dlMonth']."',
'".$_POST['dlDay']."', '".$_POST['dlYear']."', '".$_POST['dlTime1']."', '".$_POST['dlTime2']."',
'".$_POST['location']."', '".$_POST['attend']."', '$cbItems_array', '".$_POST['taAdd']."')";

or alternitively just use $cbItems in the SQL statment instead of $cbItems_array
Doing this just inserts the word "Array":

$cbItems = array($_POST['items']);

include 'db_connect.php';

$query_insert = "INSERT INTO healthed_workshop (date, fname, lname, phone, email, org, dlSubject,
other, dlMonth, dlDay, dlYear, dlTime1, dlTime2, location, attend, cbItems, taAdd)".
"VALUES (now(),'".$_POST['fname']."', '".$_POST['lname']."', '".$_POST['phone']."', '".$_POST['email']."',
'".$_POST['org']."', '".$_POST['dlSubject']."', '".$_POST['other']."', '".$_POST['dlMonth']."',
'".$_POST['dlDay']."', '".$_POST['dlYear']."', '".$_POST['dlTime1']."', '".$_POST['dlTime2']."',
'".$_POST['location']."', '".$_POST['attend']."', '$cbItems', '".$_POST['taAdd']."')";
-------------------------------------------------

When debugging a quick

echo("<pre>\n");
print_r($_POST);
echo("</pre>\n");

is always handy.
I am using that echoing trick and I didn't paste the echoed error here only b/c it's just showing that the values for $items is empty.
Jul 27 '06 #3

P: 6
If $items is no longer an array by the time it reaches my last page, should I use another foreach() construct like I did on my 2nd page? I'm not having any luck with using one there. Thanks.
Jul 27 '06 #4

bevort
P: 53
In the first page you use
foreach ($_POST['cbItems'] as $items)
to show the content of the array.
After this ForEach the last value is in $Items.

Then you create a new var cbItems in your hidden input with the value $_POST['$items']. this would be the value of the last var in $_POST. Sending this page results in receiving $_POST['cbItems'] with the last value of your previous but now gone var $_POST[cbItems] from your first page. This is not an array anymore.
If your users dit not check anything the $Items in the input would be NULL.

In page 2 you create a var cbItems as an array with the number of fields set in the input statement. You can absolutely not know how many field you will get here but the are empty. WHatever you do with implode or explode, empty is empty.

Storing data in MySQL using an array is not my favorite. I would use a while loop to include the content of the array as separate strings or integers.

I never tested to store an array in an hidden input field but if this works place the input in the ELSE part of your first check. and asign the value $_POST['cbItems'] to it to keep the original data. Then make a loop to generate the SQL statement. otherwise create using PHP hidden inputfiels for each value in your array

Put, when developing in every loop and IF structure fitting messages like
Echo();
Print-r();
to check your data

just before sending the SQL show it in an echo aswel to see how your statement looks for MySQL.

hope this helps

Vincent
Jul 30 '06 #5

P: 6
I should have came back to post when I fixed this problem. Thanks for your reply, though. Here is what I did:

I imploded on my confirmation page instead of my last page -

<?php
if(!isset($_POST['cbItems']))
{
//array is empty
$_POST['cbItems'] = array();
echo "None selected.";
}
else
{
//must do this for checkboxes (passing multiple values)
foreach ($_POST['cbItems'] as $cbItems)
{
echo " - $cbItems";
}
}

$cbItems = $_POST['cbItems'];
$cbItemsString = implode(",",$cbItems);
?>
<input type='hidden' name='cbItems' value='<?php echo $cbItemsString;?>'>

Then I inserted this on my last page -

'".$_POST['cbItems']."'
Jul 31 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.