In a quiz app, I am generating Radio options for each question dynamically from database. I need to store the chosen answer of the questions the user submitted in a database table. I want to know which user picked which answer for which question.
Here is my form:
<form id="question" class="" action="quiz_ans.php" method="post">
<table id="quiz-question" align="center" class="row-border compact order-column stripe">
<input class="form-control" type="hidden" name="NumberofQuestions" id="NumberofQuestions" value="<?php echo $NumberofQuestions; ?>">
<thead>
<?php
if($QuizQuestions) {
$i=1;
foreach($QuizQuestions as $row):
?>
<tr>
<th><?php echo $i; ?>. <?php echo $row->Question; ?>
<br>
<?php if(isset($row->Screenshot)) { ?>
<img src="<?php echo htmlspecialchars($row->Screenshot); ?>" alt="test" height="300" width="980">
<?php } ?>
</th>
</tr>
</thead>
<tbody>
<?php if(isset($row->Option1)) { ?>
<tr class="info">
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="0"><?php echo $row->Option1; ?></td>
</tr>
<?php } ?>
<?php if(isset($row->Option2)) { ?>
<tr class="info">
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="1"> <?php echo $row->Option2; ?></td>
</tr>
<?php } ?>
<?php if(isset($row->Option3)) { ?>
<tr>
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="2"> <?php echo $row->Option3; ?></td>
</tr>
<?php } ?>
<?php if(isset($row->Option4)) { ?>
<tr>
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="3"><?php echo $row->Option4; ?></td>
</tr>
<?php } ?>
<tr>
<td><label for="AnswerReason">Why?</label><input class="form-control" type="text" name="AnswerReason[]" id="AnswerReason" value=""></td>
</tr>
<?php if(isset($row->Id)) { ?>
<tr>
<td><input class="form-control" type="hidden" name="QuestionId[]" id="QuestionId" value="<?php echo $row->Id; ?>"></td>
</tr>
<?php } ?>
</tbody>
<?php
$i++;
endforeach;
}
?>
</table>
<br>
<input type="submit" name="submit" value="Submit" class="btn btn-success">
</form>
This is how I tried to store the data in database table:
$NumberofQuestions = $_POST['NumberofQuestions'];
for ($i=0; $i<$NumberofQuestions; $i++)
{
$sql = "INSERT INTO tblquizresponse (QuestionId, AnswerId, AnswerReason, UserId, SubmittedAt) VALUES (' ".$_POST['QuestionId'] [$i]." ',
' ".$_POST['AnswerId'] [$i]." ', ' ".$_POST['AnswerReason'] [$i]." ', ' ".$UserId." ', ' ".$SubmittedAt." ')";
$stmt = $pdo->prepare($sql);
$stmt->execute();
}
But I do not get the AnswerId stored in database. Here is the array I get if the form is submitted:
Array
(
[NumberofQuestions] => 5
[AnswerId] => Array
(
[16] => 0
[17] => 1
[18] => 2
[74] => 3
[75] => 0
)
[AnswerReason] => Array
(
[0] => Test answer one reason.
[1] => Test answer two reason.
[2] => Test answer three reason.
[3] => Test answer four reason.
[4] => Test answer five reason.
)
[QuestionId] => Array
(
[0] => 16
[1] => 17
[2] => 18
[3] => 74
[4] => 75
)
[submit] => Submit
)
Here is the data stored in table after submit:
Id QuestionId AnswerId AnswerReason UserId SubmittedAt
1 16 0 Test answer one reason. xxxxxxxx 2020-02-26 12:38:53
2 17 0 Test answer two reason. xxxxxxxx 2020-02-26 12:38:53
3 18 0 Test answer three reason. xxxxxxxx 2020-02-26 12:38:53
4 74 0 Test answer four reason. xxxxxxxx 2020-02-26 12:38:53
5 75 0 Test answer five reason. xxxxxxxx 2020-02-26 12:38:53
How do I get the AnswerId stored in the database as well? Any help would be much appreciated.