我正在参加培训课程申请,这对我来说是第一次,所以我遇到了一些障碍。我有一个表单,即培训评估,在提交时,会向php页面发送一系列id和所选答案进行处理。我的想法是运行一个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都是具有多个值的数组......
有人可以提供一些指导吗?我将不胜感激!
答案 0 :(得分:1)
首先,您会注意到,使用提供的HTML,用户只能从8个选项中选择一个答案。这不是您想要的:您希望每个问题允许一个选择。这可以通过给无线电控件提供不同的名称来解决:每个问题一个唯一的名称。而且你也不需要数组符号,因为每个问题你将不会有多个答案:
<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='q1' type='radio' value='A'/>This is a multiple choice answer.</li>
<li><input name='q1' type='radio' value='B'/>This is a multiple choice answer.</li>
<li><input name='q1' type='radio' value='C'/>This is a multiple choice answer.</li>
<li><input name='q1' type='radio' value='D'/>This is a multiple choice answer.</li>
</ul>
</li>
<li class='question'>This is a question?
<ul style='list-style:none;'>
<li><input name='q2' type='radio' value='A'/>This is a multiple choice answer.</li>
<li><input name='q2' type='radio' value='B'/>This is a multiple choice answer.</li>
<li><input name='q2' type='radio' value='C'/>This is a multiple choice answer.</li>
<li><input name='q2' type='radio' value='D'/>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>
因此,您不必将问题编号添加到单选按钮的 ABCD 值(您可以,但这不是必需的)。此外,我选择了较短的名称(q1,q2),因为较长的名称也会使URL长。
然后在PHP中,我建议从数组中的数据库表中读取所有正确答案,并将这些答案逐一与用户的答案进行比较,并保持计数。我在这里假设 mysqli _ 函数,但应该很容易将其转换为您喜欢的方法(如PDO):
// Connection to database...
$con = mysqli_connect("my_host", "my_user", "my_password", "my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Read all correct answers from the database:
$query = "SELECT answerID, correctAnswer FROM answerTable";
$result = mysqli_query($con, $sql);
// Fetch all records into an associative array
$correctAnswers = mysqli_fetch_all($result, MYSQLI_ASSOC);
// Count correct answers provided by user (via POST)
$correctCount = 0;
foreach ($correctAnswers as $correctAnswer) {
$answerID = $correctAnswer['answerID'];
if (isset($_POST["q$answerID"])
&& $_POST["q$answerID"] == $correctAnswer['correctAnswer']) {
$correctCount++;
}
}
// Score is fraction between 0 and 1. Multiply by 100 for a percentage
$score = $correctCount / count($correctAnswers);
请注意,这样可以避免因SQL injection而导致的漏洞,因为上面的SQL语句未通过 POST 提交的值定义。