我正在创建一个显示一组问题的网站,但是我的问题是检查用户是否回答了问题,如果回答了,则显示“已回答”标签。
但是,当前,它会显示每个问题的“已回答”标签,即使答案不在提交表中。任何帮助都将不胜感激。

while($data = mysqli_fetch_row($result)){
  if($data[0] != null){
  echo('
  <div class="col-md-4 col-sm-5">
    <div class="panel panel-default text-center">
      <div class="panel-heading">
        <span class="fa-stack fa-5x">
          <i class="fa fa-circle fa-stack-2x text-default"></i>
          <i class="fa fa-codepen fa-stack-1x fa-inverse"></i>
        </span>
      </div>
      <div class="panel-body">
        ');
        if($result4 = mysqli_query($mysqli,"SELECT * FROM submissions where teamID='$teamName' and questionID='$data[0]' and status='correct'")){
          echo "Answered";
        } else{
          echo "Not Answered";
        }

        echo ('
        <h4>'.$data[6].' - <small><i>'.$data[8].' points</i></small></h4>
        <p>'.$data[7].'</p>
        <a href="question.php?id='.$data[0].'" class="btn btn-primary btn-block">View Question </a>
      </div>
    </div>
  </div>
  ');} else{
    echo "No More Questions";
  }
}

我知道我犯了一个基本的或愚蠢的错误,所以任何帮助都值得感谢。

最佳答案

//Mysql query to find number of answers for particular question.
$answercount = mysqli_query($mysqli, "SELECT * FROM submissions where teamID='$teamName' and questionID='$data[0]' and status='correct'");
$answercount = mysqli_num_rows($answercount);

//PHP code
if($answercount>0){
    echo "Answered";
} else{
    echo "Not Answered";
}

08-27 04:03