Click to See Complete Forum and Search --> : Using serialize to insert checkbox data


rbragg
07-25-2006, 11:26 AM
I have this checkbox group:

<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

I want to send the checkbox values to the database using serialization, as well as other normal values.

<?php
include 'db_connect.php';

$cbItems = serialize($_POST['cbItems']);

$query_insert = "INSERT INTO healthed_workshop (date, fname, lname, cbItems)".
"VALUES (now(),'".$_POST['fname']."', '".$_POST['lname']."', '$cbItems')";

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

mysql_close;
?>

Everything comes up fine but in the cbItems column, I still get the array keyword:

s:5:"Array";

I get the number 5 no matter how many or if none are selected. Obviously, I'm not using this "serialize" feature correctly?

chazzy
07-25-2006, 12:10 PM
is the function implode what you're looking for? maybe explode?

either way, you shouldn't store an array like that in a database. use a separate table to store each entry as it's own line and have it reference a foreign key to another table, it's harder to search this way.

rbragg
07-25-2006, 02:19 PM
No, it does not implode what I'm looking for, nor does it look serialized. I'm guessing that it should not be imploding the word "Array" and that this suggests a syntax problem.

I won't be allowing search capabilities for this table using certain criteria. I'll only be displaying the contents as a whole so I'm not sure that using a foreign key is necessary? Is there a way to use INSERT command with the foreach() construct?

OR

Could I just use a different column within my database for each item in cbItems?

chazzy
07-25-2006, 02:39 PM
I think implode is exactly what you want


<?php

$array = array('příjmení', 'e-mail', 'telefon');
$oddelene_carkami = implode(",", $array);

echo $oddelene_carkami; // příjmení,e-mail,telefon

?>

serialize will make it a string representation of the object, so that when you unserialize it is available again.

you should almost never have multiple values stored in a single column, it's just bad design. sure you don't have a requirement now but you might one day.

rbragg
07-25-2006, 02:57 PM
With your sample code in mind - would I have a different name for each of my checkboxes and therefore have a separate column in my db for each of them? Is implode just used to bring all of these together for more simple echoing and handling? If I'm following, I could use this sample code to later retrieve my checkbox information from my db?

Maybe I'm missing the big picture.

chazzy
07-25-2006, 05:12 PM
With your sample code in mind - would I have a different name for each of my checkboxes and therefore have a separate column in my db for each of them? Is implode just used to bring all of these together for more simple echoing and handling? If I'm following, I could use this sample code to later retrieve my checkbox information from my db?

Maybe I'm missing the big picture.

Think about it this way.

if 0,2,3,4 are checked, the array looks like $array[0]=$zeroval; $array[1]=$twoval; etc and if 1,4,6,7 are set, they have the same positions but different vals. you should be using implode to store it as a string since that's what you want to do based on your post. Serializing it and storing it in a database might not be what you want.

If you implode then store in a database, you would explode the database entry to read it. if you serial and store in a database, you would unserialize and read the entry. They do basically the same thing, one is a string representation of an array, the other is a serialized form of an object.

rbragg
07-26-2006, 09:21 AM
I think I understand what you are explaining though I still have an error somewhere. On my confirmation page 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 $cbItems)
{
echo " - $cbItems";
}
}
?>
<!-- this passes the variable to the next page to be inserted in db -->
<input type='hidden' name='cbItems' value='<?php echo $_POST['$cbItems'];?>'>
---------------------

On the page where I connect and insert into the db I have this:

---------------------
<?php
$cbItems = array('item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8');
$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']."', '".$_POST['array']."', '".$_POST['taAdd']."')";

echo "$query_insert";

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

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

My echo displays all of the inserted values except for $array and in my db I have an empty column for cbItems. Am I applying this correctly?

edited to say: cbItems is type varchar(20) in my table

chazzy
07-26-2006, 12:17 PM
you have a variable named $array. I don't think you have a variable named $_POST['array'] anywhere.

rbragg
07-26-2006, 12:44 PM
LOL! Well, that would make sense!

<?php
$cbItems = array('item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8');

$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']."', '$array', '".$_POST['taAdd']."')";

echo "$query_insert";

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

mysql_close;
?>

However, my echo says I'm sending every item, whether or not these items are checked. Does this mean I must use explode() even before I plan on echoing/printing cbItems? I'm fidgeting with the syntax as you read this post.

chazzy
07-26-2006, 02:56 PM
your array has nothing to do with the items checked or not, as per the start of your script

$cbItems = array('item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8');

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

see, they are the hardcoded item1,item2, etc.

rbragg
07-26-2006, 04:08 PM
mmhmm, definitely hard-coded. I've been trying a few things that give me some php errors. Since I want to only deal with the submitted choices this means that I'm not wasting my time on another foreach() construct ... or am I?

rbragg
07-27-2006, 03:30 PM
I have an empty value with this:

<?php

$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;
?>

The echo also shows an empty value. :confused:

chazzy
07-27-2006, 03:45 PM
do this

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

I'm assuming that either the fields aren't named "items[]" or an array of an array will give problems w/ implode.

rbragg
07-27-2006, 03:52 PM
Thanks for not giving up on me.

Interesting. I get this:

---------------------
Thank you for submitting a presentation request. Array ( [0] => ) INSERT INTO healthed_workshop (date, ...
---------------------

... on down to my where my array is. It is empty.


In my form, my fields are named "cbItems[]". But, on my confirmation page it is changed when I set up my array.

<?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'];?>'>

That's why on the last page I'm dealing with $items.

chazzy
07-27-2006, 04:11 PM
you don't seem to understand - the $_POST array is a list of fields sent based on their HTML field names. cbItems is not the same as items, they must have the same name.

rbragg
07-27-2006, 04:27 PM
Now I'm passing $cbItems to my last page instead of $items.

There I have:

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

I try to insert $cbItems_array and I get the same result. If I try to insert $cbItems, I get the word "Array".

chazzy
07-27-2006, 06:15 PM
nope
it's just $_POST['cbItems'], not $cbItems...

rbragg
07-28-2006, 07:51 AM
I see how that would be incorrect. Now I am passing via a hidden field:

<input type='hidden' name='cbItems' value='<?php echo $_POST['cbItems'];?>'>

On my last page:

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

And I'm inserting:

$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']."')";

I still get an empty string. Just so you know, I am constantly troubleshooting this between our posts.

chazzy
07-28-2006, 08:27 AM
I don't think you're getting it.

What happened to your checkboxes?? What are they named??

rbragg
07-28-2006, 08:40 AM
As I mentioned before:

In my form, my fields are named "cbItems[]".

I have this checkbox group:

<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

chazzy
07-28-2006, 08:53 AM
right but now you just added this:

<input type='hidden' name='cbItems' value='<?php echo $_POST['cbItems'];?>'>

which is implying that your form now looks like this


<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
<input type='hidden' name='cbItems' value='<?php echo $_POST['cbItems'];?>'>


So which cbItems is $_POST['cbItems'] supposed to represent?

rbragg
07-28-2006, 09:35 AM
Ok, I don't know why I was being so hard-headed about this:

<input type='hidden' name='cbItems' value='<?php echo $_POST['cbItems'];?>'>

name='cbItems' //being my new field name so I needed to also pass 'cbItems' in $_POST[ ];

I should get in the habit of naming my variables differently than my field names to prevent that confusion.

Now, on my last page I have this:

$items= array($_POST['cbItems']); //with my new habit of naming variable differently than the field name
$cbItems_array = implode(",", $items);

And I'm inserting, $cbItems_array. I am getting the word "Array".

chazzy
07-28-2006, 09:58 AM
just so i can understand and you can understand then...

On page 1, you have the checkboxes.
And on the page that processes page1 you have

$cbItems = $_POST['cbItems'];//this is an array already
$cbItemsString = implode(",",$cbItems);//now they're in string form
?>
<input type='hidden' name='cbItems' value='<?php echo $cbItemsString;?>'>

Make sense? So now you're sending a field type hidden back to the processor, that will contain the string version of cbItems ALREADY when you reference $_POST['cbItems'] so you shouldn't need to implode the field.

What I'm confused about was when you started this topic, I thought you were talking about a single page processor. Now it seems like you're having a multistep processor - is that it? Please explain what you're trying to do. don't be vague.

rbragg
07-28-2006, 10:08 AM
I apologize b/c this problem is an extension of a previous problem that I posted in another thread. In that thread I explained that I have 3 pages. I should have made that more clear in this new thread.

Page 1 - 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

Page 2 - Confirmation (echoing what the user has entered so they can confirm;upon submit, this page is passed to Page 3)

<?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";
}
}
?>
<input type='hidden' name='cbItems' value='<?php echo $_POST['cbItems'];?>'>

Page 3 - Thanks (gives a thank you message and most importantly, connects to the db and inserts)

<?php

// I do this b/c its my understanding that I'd lose the array between pages
$items= array($_POST['cbItems']);
$cbItems_array = implode(",", $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_array', '".$_POST['taAdd']."')";

echo "$query_insert";

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

mysql_close;
?>

chazzy
07-28-2006, 11:31 AM
You didn't implode it though! That's why you're getting "Array". If you did <?php
echo array();
?> You would just get Array also. you need to implode it to echo it like i mentioned before. You can't pass it like that over HTTP, the parsing as an array is on the PHP side, but not on the HTML/HTTP sides so it doesn't know what to do w/ it.
suggestion is to convert it to string on the confirmation page, then when they post to the final page it's already a string. Use my example from above.

rbragg
07-28-2006, 12:50 PM
And I should explode() on the final page before I insert them, correct?

chazzy
07-28-2006, 12:59 PM
And I should explode() on the final page before I insert them, correct?
Why? They're already in string form and you just want to insert the string, right?

rbragg
07-28-2006, 01:31 PM
I didn't think I needed explode() but my value was empty. HOWEVER, I was trying to insert VALUE '".$_POST['cbItemsString']."' When I changed that to '".$_POST['cbItems']."' it worked beautifully!!

Thanks for sticking with me! :D

chazzy
07-28-2006, 04:09 PM
why would it be cbItemsString...you don't have a form field named that do you? :-D