2
<?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 type="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 ?

1
  • Why are you saving a (whatever delimited) list in an SQL database? Set up a many-to-many relationship with a bridge table, then you'll be able to query the values properly. Commented Jul 29, 2011 at 9:48

3 Answers 3

2

Use cycle to create string you need

$category = '';

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

$ins=mysql_query("insert into fruits (`category`) values ('$category') " ) ;
Sign up to request clarification or add additional context in comments.

1 Comment

@Quentin, yeah it's fun. But it just was a string from author code.
2

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.