0

I am almost at the end of creating a basic quiz application. As a new programmer, I know that I will make lots of mistakes but I want to learn from them. So I have been getting a few hiccups and the latest one is that my score variable is not being set correctly.

I am getting zero mostly for the result score at the end of the quiz.

I don't know what the errors are. This is why I will be very grateful for any help or suggestions. Thank you very much indeed.

Here is a basic run through of my code:

Question.php

<?php include "database.php"; 
?>
<?php session_start(); ?>

<?php
//Set question
$number = (int) $_GET['n'];


    /*
 * Get Total Questions
*/
 $query="SELECT * FROM questions" ;
 //Get Results
$results = mysqli_query ($conn,$query);
$total = mysqli_num_rows($results);



/*
* Get the Question
*/
$query = "SELECT * FROM questions
WHERE question_number = $number";

//Get result
$result = mysqli_query ($conn,$query);

$question = $result->fetch_assoc();


/*
* Get choices
*/
$query = "SELECT * FROM choices
WHERE question_number = $number";

//Get result
$choices = mysqli_query($conn,$query);


?>

 <?php echo $question ['text']; ?>
</p>
 <form method="post" action="process.php"
  <ul class="choices">

<?php while ($row = $choices->fetch_assoc()) : ?>
<li><input name="choice" type="radio" value="<?php echo $row ['id'];?> " /><?php echo $row ['text']; ?></li>
      <input type="submit" name="submit" value="Sumbit"/>
      <input type="hidden" name= "number" value="<?php echo $number; ?>" />



 </main>

    </body>
</html>

<?php endwhile; ?>

Process.php

<?php include "database.php"; 
?>
<?php session_start(); ?>
<?php
// check to see if score is set error_handler)
if(!isset($_SESSION['score'] )) {
    $_SESSION ['score'] = 0;
}   

if($_POST) {
    $number = $_POST['number'];
    $selected_choice = $_POST['choice'];
    $next = $number+1;


    /*
 * Get Total Questions
*/
 $query="SELECT * FROM questions" ;
 //Get Results
$results = mysqli_query ($conn,$query);
$total = mysqli_num_rows($results);



    /*
    *   Get correct choice
    */
    $query = "SELECT * FROM choices
 WHERE question_number = $number AND is_correct=1";

//Get result
$result = mysqli_query($conn,$query);

//Get Row
$row = $result->fetch_assoc(); 

//Set Correct choice
$correct_choice = $row['id'];


//Compare choice
if ($correct_choice = $selected_choice) {
    //The answer is correct
    $_SESSION['score']+1 ;
}

//Check if it is the last question
if($number == $total){
    header("Location: final.php") ;
    exit();
    }  
    else { 
      header("Location: question.php?n=".$next);

    }

<p>Final score: <?php echo $_SESSION ['score']; ?> <p>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Amrd2013
  • 13
  • 1
  • 5

2 Answers2

2

You're not actually incrementing the score, change:

$_SESSION['score']+1;

To this:

$_SESSION['score'] += 1;

This is shorthand for:

$_SESSION['score'] = $_SESSION['score'] + 1;
iJamesPHP2
  • 524
  • 4
  • 13
0

This is wrong:

$_SESSION['score']+1 ;

This calculates the new score but doesn't do anything with it. It doesn't update the session variable. It should be:

$_SESSION['score']++;

The ++ increment operator reassigns the variable with its value + 1.

You also have a typo on this line:

if ($correct_choice = $selected_choice)

= is for assigning, == is for comparing, so it should be

if ($correct_choice == $selected_choice)
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks a lot for your help. However unfortunately its still not working sadly – Amrd2013 Feb 22 '20 at 09:54
  • Check your log for "Headers already sent" warnings. If you see them, see https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php and fix the problem. – Barmar Feb 22 '20 at 14:51
  • I don't think I got that warning, but I'll check anyway thank you – Amrd2013 Feb 22 '20 at 16:44
  • Is the session variable being set in the first place? I can't think of a reason why incrementing it wouldn't work. – Barmar Feb 22 '20 at 16:47
  • Are you sure the `if` condition is succeeding? – Barmar Feb 22 '20 at 16:47
  • So I think what I have done is set the score to default if it is already not set (to another score). Also I think that the if statement works as it redirects me to the next question. But I'm not sure why it doesn't work. Thank you very much for your help once again. Update: after using a single equal sign for the selcted choice statement It is increasing the score every time by 2 for some reason.. – Amrd2013 Feb 22 '20 at 16:50
  • I'm talking about `if ($correct_choice = $selected_choice)`, since that's when you increment the session variable. – Barmar Feb 22 '20 at 16:51
  • Ah ok, sorry for the missuderstanding. So after using one equal sign it increases the score every time by 2 and if the statement is used with 2 equals signs then it does not increase the score at all. – Amrd2013 Feb 22 '20 at 16:54
  • `=` is for assigning, `==` is for comparing. It should be `if ($correct_choice == $selected_choice)` – Barmar Feb 22 '20 at 16:57
  • Oh ok thank you very much. As you can see I am a novice to programming therefore am finding it difficult to make code work. As for using = in the if statement it doesn't increase the score. It only leaves the score as 0. Thanks – Amrd2013 Feb 22 '20 at 17:02
  • It depends on what the choice is. If the choice is `0` it won't increment the variable, if the choice is non-zero it will increment the variable. – Barmar Feb 22 '20 at 17:04
  • Oh ok thank you very much. I hopefully will be able to solve this issue, thanks anyway again. – Amrd2013 Feb 22 '20 at 18:05