473,390 Members | 1,438 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,390 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 18420
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.