gpt4 book ai didi

php - 从数据库中获取答案

转载 作者:行者123 更新时间:2023-11-29 13:48:24 24 4
gpt4 key购买 nike

我正在制作一个在线测试脚本,您可以在输入元素中输入答案。提交测试后,我希望将数据库的答案与输入的答案进行比较,以确定它是否错误,但是我使用的脚本不起作用! :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']

所以你应该使用列名,而不是表名

关于php - 从数据库中获取答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17087933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com