Forum Moderators: coopster

Message Too Old, No Replies

Database entry from multiple checkbox form data

         

jabraltar

8:40 pm on Nov 17, 2006 (gmt 0)

10+ Year Member



I followed this post: [webmasterworld.com...]
and finally got my multiple checkbox selection to post in my database WITHOUT say "ARRAY."

HOWEVER...

it only displays the last entry from my checkbox selections?

here's the code, identical to the one from the post above:

***********************************************
from my form:

<input type="checkbox" value="dog" name="test[]"> Dog<br>
<input type="checkbox" value="cat" name="test[]"> Cat<br>
<input type="checkbox" value="horse" name="test[]"> Horse<br>

From my php:

if(isset($_POST["test"])) {$test = $_POST["test"];} else {$test=array();}

for ($i="0"; $i<count($test); $i++) {
if(!is_numeric($test[$i])) {$test[$i]="";}
if(empty($test[$i])) {unset($test[$i]);}}

$test = implode ("<>", $test);
$test = "<>".$test."";

mysql_query("INSERT INTO `my_table` VALUES ('$test')")

***********************************************
so if I select all the options in my form, in my database I get the only value in my column is "<>horse", where I want it to be: "<>dog<>cat<>horse"

ANYONE?

dreamcatcher

8:53 pm on Nov 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jabraltar,

A warm welcome to Webmaster World. Try this:


$string = '';

if (!empty($_POST['test']))
{

for ($i=0; $i<count($_POST['test']); $i++)
{
$string .= '<>'.$_POST['test'][$i];
}

}

mysql_query("INSERT INTO `my_table` VALUES ('$string')")

dc

jabraltar

9:01 pm on Nov 17, 2006 (gmt 0)

10+ Year Member



Wow... that's amazing! Thank you dreamcatcher. I searched for hours and could not find anything that would work not to mention so simple.

Thank you!

dreamcatcher

9:06 pm on Nov 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You`re welcome. As you are using checkboxes with values, the array will never contain empty values. Therefore you don`t need to check if a slot is empty or not in your loop. If its not checked it won`t be set. Hope that made some kind of sense.

dc