-1

I have SQL database which has a table (results) that is gonna save all of the results of my survey.

One of the questions is checkbox (multiple answer), so I made a code that should gather the results of checkbox questions and put its values into the database.

But, something's missing, it overwrites the last value in the array so obviously there is an easy solution which I cannot see. To clarify, I want to store all values of one checkbox question if user checks everything.

HTML FORM

<input type="checkbox" name="checkbox1" value="This is the first value">
<input type="checkbox" name="checkbox1" value="This is the second value">
<input type="checkbox" name="checkbox1" value="This is the third value">

PHP SCRIPT

for($i=1;$i<101;$i++) {
    if(isset($_POST['checkbox'.$i])) {
        $q[$i] .= $_POST['checkbox'.$i].'; ';
    }}

<?php
$db =& JFactory::getDBO(); 
$query = "INSERT INTO jos_results(question1) VALUES ('$q[1]')"; 
$db->setQuery($query); 
$db->query();
?>
mpavlovic89
  • 749
  • 3
  • 16
  • 37
  • It isn't duplicate, the example is with different names. My point is that in my code, the names are same. – mpavlovic89 Apr 22 '14 at 18:04
  • Yes, but the logic is the same. See the answer below. – Amal Murali Apr 22 '14 at 18:05
  • @mpavlovic89 The selected answer on that duplicate explains multiple strategies for approaching this problem, including adding `[]` to the name to get proper array access in PHP. – Mike Brant Apr 22 '14 at 18:05

1 Answers1

0

If the name of check box type is same then use array then only it will work. like here

<input type="checkbox" name="checkbox1[]" value="This is the first value">
<input type="checkbox" name="checkbox1[]" value="This is the second value">
<input type="checkbox" name="checkbox1[]" value="This is the third value">
Sumit
  • 698
  • 4
  • 11