0

I have an input array checkbox with id values. (I'm not sure if the array is working)

<?php $resource=mysql_query("Select * from material_rec",$con); ?>
    <?php while($result=mysql_fetch_array($resource))
        { ?>

    <input type="checkbox"  name="checkbox1" id="checkbox1[]" value="<?php echo $result['id']?>" />

    <?php };?>

when i insert the value of my checked checkboxes. it only insert one value and doesn't insert my selected checkboxes.

How can i insert the array values from the selected checkboxes into one colum with comma separtor?

user3117337
  • 213
  • 1
  • 5
  • 17

2 Answers2

2

It should be

name="checkbox1[]"

Whereas you have

id="checkbox1[]"

Then when inserting the values in the database, you can do

$values = implode(",", $_POST["checkbox1"]);
Ben Fortune
  • 31,623
  • 10
  • 79
  • 80
0

For inserting values of a checkbox can also loop through the array:

$check=filter_var($_POST['checkbox1'], FILTER_SANITIZE_STRING);

foreach($check as $value) 
  {
     $insert_table=$conn->prepare("insert into table values (?);");
     $insert_table->execute(array($value)); 
  }
digitai
  • 1,870
  • 2
  • 20
  • 37
  • You only need to prepare the statement once, even then you don't want to be referencing the variable directly in the query. – Ben Fortune Jan 23 '14 at 16:28
  • This is interesting and new to me, I already do this way, but as your comment suggests it should better prepare once. I'm eager to kmow more about this. Do you have a link where I can check this. And thanks, this comment will help me to improve my queries. – digitai Jan 23 '14 at 16:31
  • The accepted answer [here](http://stackoverflow.com/questions/9168562/pdo-cost-of-calling-prepare-in-a-loop) explains it pretty well. – Ben Fortune Jan 23 '14 at 16:35