View Full Version : Entering data into DB field with multiple values?

03-23-2009, 08:32 PM
Hi All,

I'm trying to work out how I can post some data to my database using a | pipe.

So for example I have a simple shopping list, and I want the user to select if they have bought the item or not. Yes have a value, no does not.


Now as you will see each item has a value, so if the first two items were bought only (bread & cheese) in my database i'd post the following values in my database column:


But how do I do this?

Normally I only post of Value from one set of radio buttons, so i'd do something like:

if (isset($_POST['Submit']))
$subsection = $_POST['subsection'];
$result = mysql_query("Insert into db (subsection) values ('$subsection')") or die(mysql_error());

But i'm trying to get different values from lots of radio buttons named with a increment on them, I.e. subsection(i++)and post them in one database column, so how would I do it?

Is this possible



03-23-2009, 08:43 PM
If these are all from a _POST field, you can use an implode if you're expecting an array. To that extent, create an array of you're radio buttons (ie: name="subsection[0]"... name="subsection[1]"...). This lets you use a foreach and implode on the $_POST['subsection'] item. I would also suggest using a checkbox instead of a radio button, especially since the radio buttons do not have a default value.

But, and I can't stress this enough, if you're using a delimiter there is something wrong with you're database schema and should be remodeled. A general goal is for BCNF (boyce codd normal form, spelling may be wrong on the names), but the very least you should have is a 3NF level for you're normalization. What you have here is a 2NF format which requires extensive querying to handle update cascade / deletions based on a specified id value (that is, if you delete a product ID of 161, you need to query each record that contains it, extract it, and replace the string). This is called a deletion anomaly, which you should be trying to prevent with practical design techniques.

03-23-2009, 08:51 PM
You can normalize your database structure or you can serialize input array and store it in one field:

$input = array();
$input[] = 100;
$input[] = 161;
$serialized = serialize($input);

When you retrieve data from database, unserialize() it.

03-24-2009, 01:20 PM
Hi stfc_boy
Is this what you need?

if (isset($_POST['Submit'])){
if($count > 0){
foreach($subsections as $subsection){
$result = mysql_query("Insert into db (subsection) values ('".$values."')") or die(mysql_error());

<form name="test" action="" method="post">
<input type="radio" class="checkbox" name="subsection[0]" value="100" /> Yes
<input type="radio" class="checkbox" name="subsection[0]" value="" /> No
<input type="radio" class="checkbox" name="subsection[1]" value="161" /> Yes
<input type="radio" class="checkbox" name="subsection[1]" value="" /> No
<input type="radio" class="checkbox" name="subsection[2]" value="171" /> Yes
<input type="radio" class="checkbox" name="subsection[2]" value="" /> No
<input type="radio" class="checkbox" name="subsection[3]" value="180" /> Yes
<input type="radio" class="checkbox" name="subsection[3]" value="" /> No
<input type="radio" class="checkbox" name="subsection[4]" value="308" /> Yes
<input type="radio" class="checkbox" name="subsection[4]" value="" /> No
<br />
<input name="Submit" type="submit" value="Submit" />