我正在制作一个在线测试脚本,您可以在输入元素中输入答案。一旦测试提交,我希望数据库的答案与输入的答案进行比较,说它是否错误,但我正在使用的脚本是不工作!: 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>
我该如何解决这个问题?
获取如下查询:
SELECT a1 FROM answer
将返回$row['a1']
,而不是$row['answer']
应该使用列名,而不是表名