...

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



stfc_boy
03-23-2009, 07: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.

http://www.inspireaway.co.uk/test3.php

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:

100|161|

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

Thanks

Chris

Fou-Lu
03-23-2009, 07: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.

steelaz
03-23-2009, 07: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.

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


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


<form name="test" action="" method="post">
<h3>Foods</h3>
<p><b>Bread</b></p>
<input type="radio" class="checkbox" name="subsection[0]" value="100" /> Yes
<input type="radio" class="checkbox" name="subsection[0]" value="" /> No
<p><b>Cheese</b></p>
<input type="radio" class="checkbox" name="subsection[1]" value="161" /> Yes
<input type="radio" class="checkbox" name="subsection[1]" value="" /> No
<p><b>Crackers</b></p>
<input type="radio" class="checkbox" name="subsection[2]" value="171" /> Yes
<input type="radio" class="checkbox" name="subsection[2]" value="" /> No
<p><b>Ham</b></p>
<input type="radio" class="checkbox" name="subsection[3]" value="180" /> Yes
<input type="radio" class="checkbox" name="subsection[3]" value="" /> No
<p><b>Beef</b></p>
<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" />
</form>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum