Php 从数据库中获取答案
我正在制作一个在线测试脚本,您可以在其中的输入元素中输入答案。提交测试后,我希望将数据库的答案与输入的答案进行比较,以说明是否错误,但我使用的脚本不起作用!: 问题就在这里! 在数据库中,我有50个现成答案中的4个(还不是全部),当我回答4个正确或错误时,返回的是不正确的答案。它列出了所有答案,无论它们在页面中是正确的还是不正确的,但它工作不正常,无论我做什么输入,它都会显示所有答案,直到49个都说不正确,然后出于某种原因,50个说正确 这是我的剧本:Php 从数据库中获取答案,php,html,mysql,mysqli,compare,Php,Html,Mysql,Mysqli,Compare,我正在制作一个在线测试脚本,您可以在其中的输入元素中输入答案。提交测试后,我希望将数据库的答案与输入的答案进行比较,以说明是否错误,但我使用的脚本不起作用!: 问题就在这里! 在数据库中,我有50个现成答案中的4个(还不是全部),当我回答4个正确或错误时,返回的是不正确的答案。它列出了所有答案,无论它们在页面中是正确的还是不正确的,但它工作不正常,无论我做什么输入,它都会显示所有答案,直到49个都说不正确,然后出于某种原因,50个说正确 这是我的剧本: <?php $con=mysqli
<?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.
获取查询,如:
SELECT a1 FROM answer
将返回$row['a1']
,而不是$row['answer']
因此,您应该使用列名,而不是表名,它仍在返回,好像它是错误的。可能是数据库吗?
SELECT a1 FROM answer