php - 使用 PDO 在 MySQL 中存储动态单选选项值数组
问题描述
在一个测验应用程序中,我从数据库中动态地为每个问题生成单选选项。我需要将用户提交的问题的选择答案存储在数据库表中。我想知道哪个用户为哪个问题选择了哪个答案。
这是我的表格:
<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>
这就是我尝试将数据存储在数据库表中的方式:
$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();
}
但我没有将 AnswerId 存储在数据库中。如果提交表单,这是我得到的数组:
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
)
这是提交后存储在表中的数据:
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
如何获取存储在数据库中的 AnswerId?任何帮助将非常感激。
解决方案
推荐阅读
- javascript - Ajax 长轮询不会在中止时停止我的 PHP 脚本
- php - 如何获取分类的自定义字段值?
- javascript - chrome.storage.sync.set a key in object in Javascript
- api - 哪种技术提供 Instagram 个人资料照片缩放功能?
- docker - 错误“无法使用前端 dockerfile.v0 解决”
- python - 如何从时区字符串列中获取时区字符串?
- python - 基于另一列的子组向 Pandas 数据框添加新列的有效方法
- typescript - NX 是否可以自定义新的应用程序文件夹结构?
- javascript - 将字段添加到 Javascript/PHP 表单 WORDPRESS
- r - R中的桑基图标签