473,395 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

storing checkbox data using implode

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
5 18421
Banfa
9,065 Expert Mod 8TB
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
rbragg
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
rbragg
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
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
rbragg
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

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

Similar topics

4
by: Bee | last post by:
Hi everyone, I'm having trouble accessing a Mysql database containing checkbox data that is posted from a form. I use the serialize function to store the checkbox values in an array (in a column...
0
by: Eric Schittlipz | last post by:
Storing redundant data is great - not only can it vastly increase the speed of certain queries, but it also leaves you with an enjoyably guilty feeling. Like the one you used to get as a kid when...
3
by: ASzasz | last post by:
I am new to VB .NET: I can configure a SQL adapter and access data via the datagrid. But how do you access and manipulate data if you do not require the data to show up on a form. I simply want to...
2
by: David Garamond | last post by:
What would be the more proper way of storing birthday data? It will be used to send out birthday messages for customers ("Happy 30th birthday, Sam!"). But the date of birth is not necessarily known...
3
by: asadikhan | last post by:
Hi, I am trying to use a datalist to edit data in a MySql database as follows. For now I just want to be able to get the changes to be made on the dataset. Here is my code: using System;...
0
by: sudhaMurugesan | last post by:
Hi, I am having single column with multiple rows and i want to store all rows into a single variable (like array variable). For example, we can use array for storing multiple...
11
by: TechnoAtif | last post by:
INSERT AND UPDATE MULTIPLE CHECKBOX DATA USING PHPMYSQL OR JAVASCRIPT Hi All I want to check the multiple checkboxes update them after revisiting that page. I am taking the name as...
3
by: amollokhande1 | last post by:
Hi All, I am using Sql server 2005 as a backend for my application. I want to read/write the unicode data using sql query. When I am using insert into UnicodeData values('سي') command and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.