1

I am in the process of creating a website that displays a group of questions, however my problem is checking if the user has answered the question or not, and if they have, display an “Answered” label.

However the currently, It is displayed the “Answered” Label for each and every question, even if the answer is not in the submissions table. Any help would be appreciated.

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";
  }
}

I know I am making a basic or stupid error here, So any help appreciated.

1
  • you forget mysqli_query("") Commented Jul 26, 2016 at 10:11

2 Answers 2

1
//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";
}
Sign up to request clarification or add additional context in comments.

7 Comments

Hey, thank you for the response, Greatly appreciated, I like the method you used, but still no luck with achieving what I am looking for. With your code, I am receiving all the question as "Not Answered".
Have you replaced the $con with your $mysqi in the mysqi_query function?
Hello, Indeed I have, I am using the varable $mysqli, instead of $con. Its like its not actually using the query and checking, because when I echo out the query, it shows the correct information SELECT * FROM submissions where teamID='Admin' and questionID='121' and status='correct' Not Answered Section 1 - 100 points 2016 cyberlympics questions View Question SELECT * FROM submissions where teamID='Admin' and questionID='129' and status='correct'
What output your are getting from the mysqi_num_rows function in our code. Does it gives '0' for Everytime?
When you are executing above query in MySQL workbench or phpmyadmin, does it give correct result?
|
0

you forget mysqli_query("")

if ($result4 = mysqli_query($con, "SELECT * FROM submissions where teamID='$teamName' and questionID='$data[0]' and status='correct'")) {
  echo "Answered";
} else {
  echo "Not Answered";
}

1 Comment

Sorry I had that in, but removed it to test something, However this still did not resolve the issue. I have updated the code in the original question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.