开发者

How to add checkbox value in database?

<?php 
 if(isset($_REQUEST['submit']))
 {
        $category = mysql_real_escape_string(implode(',', $_POST['category']));

 }

$ins=mysql_query("insert into fruits (`category`) values ('$category') " ) ;

?>

<form name="form1" method="post" action="">
<input type="checkbox"    name="category[]" value="apple" >Apple
<input type="checkbox"    name="category[]" value="orange" >Orange
<input type="checkbox"    name="category[]" value="mango" >Mango
<input ty开发者_如何学运维pe="submit" name="submit" value="submit"  />
</form>

I need to save checkbox value like [apple][orange][mango] but now it save like apple,orange,mango

can anyone help me ?


Use cycle to create string you need

$category = '';

foreach ( $_POST['category'] as $cat )
{
    $category .= '[' . $cat . ']';
}

$ins=mysql_query("insert into fruits (`category`) values ('$category') " ) ;


I agree with Quentin's comment on your question - many to many with bridge table is the best way to go for this, I've had to do something very similar recently and that was the best solution.

If you are intent on doing this anyway, you'd need to just loop through the category[] array and build a string with your square brackets, something like this:

$str='';
foreach ($_POST['category'] as $val)
{
    $str.='['.$val.']';
}

Unless I've misunderstood the significance of the brackets that is :)

EDIT: Just read http://bobby-tables.com/, (courtesy of quentin) lmao. you should consider the possible security implications before going ahead with this.


Not sure I've understood you right, but try this

$category = mysql_real_escape_string(implode('][', $_POST['category']));
if($category)
   $category='['.$category.']';

I hope it will be helpfull.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜