php 为什么我的测验显示错误的“正确答案”

mgdq6dx1  于 2023-01-04  发布在  PHP
关注(0)|答案(1)|浏览(155)

我是新来的php所以我道歉如果我的问题是相当原始的。我已经做了一个测验在php中,应该发生的是测验找到正确的答案为当前显示的问题,然后,当用户提交答案时,程序应该检查是否用户提交的答案匹配正确的答案。然而,当我的测验移动时,会在正确答案获得分数之前更改“correctAnswer”变量。我不确定如何修复此问题。

include('connect_db.php');

$sqlstmt = 'SELECT * FROM quiz ORDER BY RAND() LIMIT 1';

$res = mysqli_query($dbc, $sqlstmt);

while ($row = mysqli_fetch_assoc($res)) { 
  $CorrectAnswer = $row['CorrectAnswer'];
  echo $CorrectAnswer;?>

<html>
  <head>
    <style>
    h1 {
      text-align: center;
    }
    .answer {
      display: flex;
      margin-inline: auto;
      margin-bottom: 1rem;
    }
    .button {
      margin-top: 1rem;
    }
    fieldset {
      display: flex;
      flex-direction: column;
      justify-content: center;
      width: 50rem;
      margin-inline: auto;
    }
    </style>
  </head>
  <body>
    <h1>Quiz</h1>
    <form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>" align="center" id='myForm'>
      <fieldset >
        <legend>Question:</legend><br>
        <p><?php echo $row['Question']; ?></p><br>
        <div class="answer">
          <input type="radio" id="answer1" name="answer" value="<?php echo $row['Answer1']; ?>"><br>
          <label for="answer1"><?php echo $row['Answer1']; ?></label>
        </div>
        <div class="answer">
          <input type="radio" id="answer2" name="answer" value="<?php echo $row['Answer2']; ?>"><br>
          <label for="answer2"><?php echo $row['Answer2']; ?></label>
        </div>
        <div class="answer">
          <input type="radio" id="answer3" name="answer" value="<?php echo $row['Answer3']; ?>"><br>
          <label for="answer3"><?php echo $row['Answer3']; ?></label>
        </div>
        <div class="answer">          
          <input type="radio" id="answer4" name="answer" value="<?php echo $row['Answer4']; ?>"><br>
          <label for="answer4"><?php echo $row['Answer4']; ?></label>
        </div>
      </fieldset>
      <input class="button" type="submit" value="Submit">
    </form>
    <script>
      function validateForm() {
        var radios = document.getElementsByName('answer');
        var formValid = false;

        var i = 0;
        while (!formValid && i < radios.length) {
          if (radios[i].checked) formValid = true;
          i++;        
        }

        if (!formValid) alert("You must select an option!");
        return formValid;
      }

      var form = document.getElementById("myForm");
      form.onsubmit = validateForm;
</script>
    <?php


if (isset($_POST['answer'])) {
  $selectedAnswer = $_POST['answer'];
  echo $CorrectAnswer,$selectedAnswer;
  if ($selectedAnswer == $CorrectAnswer)
    echo '+1 pt';
  
}
}
?>
  </body>
</html>

我试过改变变量声明的顺序,以为这样就能把正确答案和提交的答案匹配起来,但是我已经是第二周好好学习php了,所以我真的不知道我在做什么或者我期待什么。

iih3973s

iih3973s1#

我们不清楚您所说的奖励积分是什么意思(只有一个echo函数)。如果您能提供SQL数据库中的信息就更好了,因为您的代码可以正确地检查$selectedAnswer是否等于$correctAnswer。根据您提供的有限信息,解决这个问题的最佳方法可能是重构这部分代码:

if (isset($_POST['answer'])) {
  $selectedAnswer = $_POST['answer'];
  echo $CorrectAnswer,$selectedAnswer;
  if ($selectedAnswer == $CorrectAnswer)
    echo '+1 pt';
  
}

首先,这部分代码的语法不正确,在PHP语法下不会作为if语句进行计算:

if ($selectedAnswer == $CorrectAnswer)
    echo '+1 pt';

请将其更改为以下格式,使其符合PHP语法规则:

if ($selectedAnswer == $CorrectAnswer) {
    echo '+1 pt';
  }

另一个问题是echo $CorrectAnswer,$selectedAnswer;什么也不做。echo调用只带一个参数,需要如下 Package :echo(...)
如果你想回显这两个值,你需要join the strings together,如下所示:

echo ($CorrectAnswer . $selectedAnswer);

正如上面链接的答案中所解释的,如果你想在两者之间添加间距,你需要做进一步的修改。
另一个问题就在这里:

while ($row = mysqli_fetch_assoc($res)) { 
  $CorrectAnswer = $row['CorrectAnswer'];
  echo $CorrectAnswer;

if语句类似,您没有在PHP下正确关闭此函数,因此它在PHP语法下无法工作。以下是正确关闭此函数的方法:

while ($row = mysqli_fetch_assoc($res)) { 
  $CorrectAnswer = $row['CorrectAnswer'];
  echo $CorrectAnswer;
}

根据您的SQL数据库结构以及您希望此程序执行的操作,此答案可能对您没有帮助。如果这不能解决您的问题,请提供进一步的说明。谢谢。

相关问题