mercredi 18 mars 2020

PHP if else statement not executing code when value is true

This is for a quiz application.

This is my comparison where $number is the question number and will increase every time the user submits an answer and we will assume $totalquestions = 3 for now as the category has only 3 questions.

I've echoed both values to make sure they are what I expect them to be and done var_dump($number == $totalQuestions); which returns true when $number = 3 but does not execute my code to redirect to the finish page once all questions are done and I have no clue why.

Any help would be great as I've been staring at this problem for hours!

if($_SERVER["REQUEST_METHOD"] == "POST"){
        $number = $_POST['number'];
        $selectedChoice = $_POST['choice'];
        $next = $number+1;
        $activeCategory = $_POST['activeCategoryID'];

        $query = "SELECT * FROM `questions` WHERE CategoryID = activeCategoryID";
        $result = mysqli_query($db, $query);
        $totalQuestions = mysqli_num_rows($result);

        //Get correct choice
        $query = "SELECT * FROM `answers` WHERE QuestionID = $number AND Correct = 1";

        $result = mysqli_query($db, $query);
        $row = mysqli_fetch_assoc($result);
        $correctChoice = $row['AnswerID'];

        if($correctChoice == $selectedChoice){
            $_SESSION['geoScore']++;
        }

        //Check to see if questions have finished
        if($number == $totalQuestions){
        header("Location: finish.php");
        }else{
        header("Location: geoQuiz.php?n=".$next."&c=".$activeCategory);
        }
    }

Aucun commentaire:

Enregistrer un commentaire