0

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

这就是问题所在!在数据库中,当我回答 4 个正确或错误时,我设置了 50 个准备好的答案中的 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>

我怎样才能解决这个问题?

4

1 回答 1

1

获取如下查询:

SELECT a1 FROM answer

会返回$row['a1'],而不是$row['answer']

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

于 2013-06-13T13:14:50.013 回答