我正在制作一个在线测试脚本,您可以在输入元素中输入答案。提交测试后,我希望将数据库的答案与输入的答案进行比较,以确定是否错误,但是我使用的脚本无效! :S
这是问题所在! 在数据库中,我有50个准备答案中的4个(当时还不是全部)当我回答4个正确或错误时它返回它们是不正确的。它会列出所有答案,无论它们在页面中是否正确或不正确,但它无法正常工作,无论我做什么输入,所有答案最多49个说不正确,然后由于某种原因50说得对吗?...
这是我的剧本:
<?php
$con=mysqli_connect("localhost","dstsbsse","pass","user");
if (mysqli_connect_errno($con))
{
echo "ERROR - Failed to connect to MySQL Server. Please contact an Administrator at English In York: " . mysqli_connect_error();
}
//Set variables to hold output data and total score.
$output="";
$score=0;
//for-next loop. This means "Set n to value one. Every time through the loop (between {}) increase n by one. Do this while n is less than or equal to 50"
for($n=1;$n<=50;$n++)
{
$sql="SELECT a$n FROM answer WHERE 1";
// $sql="SELECT * FROM answer WHERE name='a$n'"; //sql is specific to your table of course - you will need to change this.
$result = $con->query($sql); // perform the query
$row = $result->fetch_assoc(); //load the result into the array $row
$key="a".$n; //concatenate to generate the $_POST keys
if($row['answer']==$_POST[$key]) //compare the data from the table with the answer
{
//answer is correct
$score++;
$output.="Answer $n is correct</BR>"; //add responses to the output string
}
else
{
$output.="Answer $n is incorrect</BR>";
}
}
$output.="Total score: $score/50"; //add the score
echo $output; //echo to screen.
以下是其中一个问题答案框的示例:
<input type="text" name="a1" id="a1" required>
我该如何解决这个问题?
答案 0 :(得分:1)
获取如下查询:
SELECT a1 FROM answer
将返回$row['a1']
,而不是$row['answer']
所以你应该使用列名,而不是表一