【发布时间】:2016-04-01 03:51:20
【问题描述】:
我正在开发一个培训课程应用程序,这对我来说是第一次,所以我遇到了一些障碍。我有一个表格,培训评估,当提交时,它会发送一组 id 和选择的答案到 php 页面进行处理。我的想法是运行一个mysql查询,选择正确答案的数量,然后另一个选择不同问题的数量,然后将两者相除以计算分数。这部分很简单,但我的思绪正在融化,试图找出选择正确答案数量的查询。
这是我的表单的代码...
<form name="assessment" method="post" action="scripts/scoreAssessment.php">
<ul class="assessment">
<li class='question'>This is a question?
<ul style='list-style:none;'>
<li><input name='trainingCourseAnswer[]' type='radio' value='A1'/>This is a multiple choice answer.</li>
<li><input name='trainingCourseAnswer[]' type='radio' value='B1'/>This is a multiple choice answer.</li>
<li><input name='trainingCourseAnswer[]' type='radio' value='C1'/>This is a multiple choice answer.</li>
<li><input name='trainingCourseAnswer[]' type='radio' value='D1'/>This is a multiple choice answer.</li>
</ul>
</li>
<li class='question'>This is a question?
<ul style='list-style:none;'>
<li><input name='trainingCourseAnswer[]' type='radio' value='A2'/>This is a multiple choice answer.</li>
<li><input name='trainingCourseAnswer[]' type='radio' value='B2'/>This is a multiple choice answer.</li>
<li><input name='trainingCourseAnswer[]' type='radio' value='C2'/>This is a multiple choice answer.</li>
<li><input name='trainingCourseAnswer[]' type='radio' value='D2'/>This is a multiple choice answer.</li>
</ul>
</li>
<!--continue pattern for total number of questions-->
</ul>
<input name='submitAssessment' type='submit' value='Finish Test'/>
</form>
在 scoreAssessment.php 中,我可以使用 PHP 的 substr 函数拆分所选单选输入答案的值,以获得两个变量,$selectedAnswer 和 $answerID。我需要运行以选择所有正确答案的查询如下...
$selectedAnswer=substr($_POST['trainingCourseAnswer'], 0, 1);
$answerID=substr($_POST['trainingCourseAnswer'], 1);
$query="SELECT COUNT(correctAnswer) WHERE answerID='$answerID' AND correctAnswer='$selectedAnswer'"
我遇到的问题是 $answerID 和 $selectedAnswer 都是具有多个值的数组...
谁能提供一些指导?我将不胜感激!
【问题讨论】:
-
"具有多个值的数组" - 由于每个单选按钮具有相同的名称,并且单选按钮的名称确定单选按钮,并且您只能在每个单选按钮中选择一个单选按钮您似乎更有可能得到一个具有一个值的数组。
-
解决方案非常简单:php.net/for
-
危险:你很容易受到SQL injection attacks的影响,你需要自己从defend那里得到。
-
正如@Quentin 所说..您需要制作两个表单和两个提交按钮..或者您需要使用 Ajax 处理一个提交按钮..在这两种情况下您都需要两个表单。跨度>
-
@ErenArdahan — 什么?我没这么说。您不需要多个表格。您需要为每组单选按钮使用不同的名称(作为奖励,这消除了拆分值的需要,因为您不需要在名称中对单选组进行编码)