gpt4 book ai didi

mysql - 保存在线测试中的答案并将其发送到数据库

转载 作者:行者123 更新时间:2023-11-30 01:12:46 27 4
gpt4 key购买 nike

我被要求编写在线测试。我有 2 个表,其中一个包含问题(2 个字段,一个用于 id,一个用于问题文本)。另一张用于保存答案的表(有 3 个字段,一个用于 id,一个用于问题 id,一个用于答案)。

到目前为止我是这样写的:

<form name="test" method="post" action="index.html">
<div class="slideshowContainer">
$question_query=dbquery("SELECT * FROM questions");
while($question_array=dbarray($question_query)){
echo'
<div class="question">
'.$question_array['question_text'].'
<br />
<textarea name="'.$question_array['id'].'"></textarea>
</div>
';
}
echo'
<input id="finish" type="submit" name="finish_test" value="send" />
</div>
</form>';

我还使用循环插件,以便用户可以以幻灯片形式查看问题,并在为每个特定问题提供的每个文本区域中写下答案。

我不能做的是我应该编写 if(isset($_POST['finish_test'])) 部分的方式。因为据我所知,这样接收到的数据是数组的形式。所以我想当我要保存答案并将其发送到我的数据库时,我可能需要另一个 for 或 while 元素。

你有什么建议?

最佳答案

当然,您需要循环遍历 $_POST 的结果,但为代码中的每个文本区域创建不同的 id 是件好事。这样您就可以识别哪些是哪些答案。为此,我会做这样的事情。

  while($question_array=dbarray($question_query)){
i = 0;
echo'
<div class="question".i>
'.$question_array['question_text'].'
<br />
<input id='question' type='hidden' value="'.$question_array['id'].'"/>
<textarea name="answer".i></textarea>
</div>';
i++;
}

现在,因为我知道 i 从 0 开始,并在问题结束时结束,所以我可以循环遍历它并访问 $_POST['answer'.i] 并进行插入。

while ($_POST['answer'.$i]) {
$i = 0;
//insert query after establishing connection, which i believe you got figured out
$sql = 'INSERT INTO ANSWERS VALUES ($_POST['answer'.$i], $_POST['question'.$i])';//Note you must have the question id in some hidden input field
$result = mysql_query( $sql, $conn );//where $conn is your connection object
//after this you might want to do something with the $result,
$i++;
}

关于mysql - 保存在线测试中的答案并将其发送到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19333591/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com