通过HTML表单向数据库添加问题时选择正确的答案

时间:2018-05-01 10:57:07

标签: php html

我有一个有问题和4个答案的表格(这是一个测验),我试着评论https://stackoverflow.com/a/29280091/6482242但我还没有足够的声誉。这与我所拥有的(以及How to set the correct answer via checkbox?)非常相似。

我的布局是:

<label for="question_text">Question Text:</label>
<textarea title="Insert Question Text" name="question_text"></textarea>

<h4>Check Correct answer -></h4>
<input type="radio" name="correct_answer_flag" value="1">
<label for="correct_answer_flag, answer_1">Answer One:</label>
<input type="text" title="Enter Answer" name="answer_1" />
<br />
<br />
<input type="radio" name="correct_answer_flag" value="1">
<label for="correct_answer_flag, answer_2">Answer Two:</label>
<input type="text" title="Enter Answer" name="answer_2" />
<br />
<br />
<input type="radio" name="correct_answer_flag" value="1">
<label for="correct_answer_flag, answer_3">Answer Three:</label>
<input type="text" title="Enter Answer" name="answer_3" />
<br />
<br />
<input type="radio" name="correct_answer_flag" value="1">
<label for="correct_answer_flag, answer_4">Answer Four:</label>
<input type="text" title="Enter Answer" name="answer_4" />
<br />
<br />
<input type="submit" value="Submit Question" name="question_submit" />

后台有一些PHP要抓住这些并将它们放入数据库,但我需要知道将单选按钮链接到文本输入的正确语法是什么?

提前致谢:D

1 个答案:

答案 0 :(得分:0)

您应该更改单选按钮的值,然后将其值存储到表格中的单独列中,其列类似于correct_answer

<label for="question_text">Question Text:</label>
<textarea title="Insert Question Text" name="question_text"></textarea>

<h4>Check Correct answer -></h4>
<input type="radio" name="correct_answer_flag" value="1">
<label for="correct_answer_flag, answer_1">Answer One:</label>
<input type="text" title="Enter Answer" name="answer_1" />
<br />
<br />
<input type="radio" name="correct_answer_flag" value="2">
<label for="correct_answer_flag, answer_2">Answer Two:</label>
<input type="text" title="Enter Answer" name="answer_2" />
<br />
<br />
<input type="radio" name="correct_answer_flag" value="3">
<label for="correct_answer_flag, answer_3">Answer Three:</label>
<input type="text" title="Enter Answer" name="answer_3" />
<br />
<br />
<input type="radio" name="correct_answer_flag" value="4">
<label for="correct_answer_flag, answer_4">Answer Four:</label>
<input type="text" title="Enter Answer" name="answer_4" />
<br />
<br />
<input type="submit" value="Submit Question" name="question_submit" />

在该栏中只存储所选电台的号码。因此,对于每个问题,您将存储4个选项,其中4个将有一个正确的答案,其索引将存储在一个单独的表中,您可以在其中存储答案选项。