0

I'm using this code to generate questions form database, it works! But i can only answer 1 question and save to database, why? If I try to answer multiple questions, the radio button value just moves around?

$question_id = mysqli_real_escape_string($con, $_POST['question_id']);
foreach ($_POST['answer_value'] as $question_id => $answer_id);

 $sql="INSERT INTO answers (question_id, answer_value)
  VALUES ('$question_id', '$answer_value')"; 

  if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
 }
 echo 'answer saved';
 mysqli_close($con);


 // generate all quetions
$query = "SELECT * FROM questions";
$result = @mysqli_query($con, $query);
echo '<form action="insert.php" method="POST">
                    Firstname: <input type="text" name="firstname">
                    Lastname: <input type="text" name="lastname">
                    Email: <input type="text" name="email"><br>';
if ($result) {
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    $body = $row['question_body'];
    $question_id = $row['question_id'];
    echo '  
        <tr>
                <td>'.$body.'</td>
                <td><input type="radio" name="answer_value['.$question_id.']" value="0"></td>
                <td><input type="radio" name="answer_value['.$question_id.']" value="1"></td>
                <td><input type="radio" name="answer_value['.$question_id.']" value="2"></td><br>

         </tr>';

    }
     echo'<input type="submit"></form>
                        <br/>';
}
}
 ?>
user3906056
  • 313
  • 1
  • 3
  • 12
  • 1
    Each radio button group needs a distinct name attribute. All of yours are sharing `name="answer_value"` – j08691 Aug 19 '14 at 14:46

4 Answers4

3

You need a separate radio group for each question. Use different names each time you go around the loop.

You can store your question id there instead of in a hidden input.

<input type="radio" name="answer_value[<?php echo $question_id; ?>]" value="1">

When this gets submitted back to the server, you will find that $_POST['answer_value'][] is an associative array with the question ids being the keys.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • When adding: My submit button is disappearing, I'm I interrupting the echo statement for the form? – user3906056 Aug 19 '14 at 15:44
  • You've used a `;` when you need a `.` – Quentin Aug 19 '14 at 15:50
  • I really don't get what would like to be the correct POST for the question_id: $answer_value = mysqli_real_escape_string($con, $_POST['answer_value'][]); $sql="INSERT INTO answers (question_id, answer_value) VALUES ('$question_id', '$answer_value')"; – user3906056 Aug 19 '14 at 18:54
  • `foreach $_POST['answer_value'] as $question_id => $answer_id` but **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Aug 19 '14 at 18:56
  • thx for the info abot sql injections. But i get an syntax error with foreach $_POST['answer_value'] as $question_id => $answer_id – user3906056 Aug 19 '14 at 19:24
  • Somehow I managed to miss the `()` off around it. See http://php.net/foreach for the proper syntax. – Quentin Aug 19 '14 at 19:52
  • Thx for your reply, i really appreciate your help. I have edited my code above. When i try your solution i'm only saving the total count of questions answered in question_id in database? How Come? – user3906056 Aug 21 '14 at 15:14
  • You need to put the stuff you want to do for each item in the array *in a block following the `foreach` statement*. – Quentin Aug 21 '14 at 15:15
0

If different radiobuttons have the same name, you can only choose one of them (you're grouping them). This is how it is mean to work, you can change to checkbox input instead:

<input type="checkbox" name="answer_value[]" value="1" />
<input type="checkbox" name="answer_value[]" value="2" />

Then you can check $_POST['answer_value'] to know ho many were checked and which value they had.

mTorres
  • 3,590
  • 2
  • 25
  • 36
0

Make each answer has the same name per question because all radio boxes with the same name are linked in html and will work as one big radiobutton group. Use a question-ID tosolve that:

<input type="radio" name="answer_value_'.$row["question_id"].'" value="1">
Steini
  • 2,753
  • 15
  • 24
  • Using a checkbox would allow multiple answers per question. That isn't the goal. – Quentin Aug 19 '14 at 14:48
  • Okok, my bad, srry. I made a little misstake understanding what the author wants. There are multiple questions, I thought he was upset about multiple answer possibilities not workin~. – Steini Aug 19 '14 at 14:50
0

The problem here is that you are looping through the $_POST['answer_value'] but only grabbing the last value.

Try the following code instead:

    foreach ($_POST['answer_value'] as $question_id => $answer_id) {
      var_dump($question_id);
    }

Inside the brackets ({}), you may do your database insertion. However, it might be a better idea to save the question and answers to a string that you'll later use in the mysql_query, to avoid unnecessary calls to the database.