php - $SESSION not working despite numerous tries and search online -
i coding online quizzer using php. whenever answerer of quiz choose correct answer, code automatically +1 overall score.
to that, used $_session['score']=0;
first set score 0 , $_session['score']++;
whenever answerer gets answer correct each question.
however, have no idea why score not adding despite answerers answering questions correctly.
when answerer answers question correctly, score still 0 reason , have no idea why. may know went wrong? thank you.
things have tried:
1.changing $_session['score']++;
$_session['score']+1;
2.changing:
if(!isset($_session['score'])){ $_session['score']=0; }
to
$_session['score']=0;
3.changing
if($correct_choice == $selected_choice){ $_session['score']++; }
to just:
if($correct_choice = $selected_choice){ $_session['score']++; }
below code process.php:
<?php include 'database.php'; ?> <?php session_start(); ?> <?php if(!isset($_session['score'])){ $_session['score']=0; } if(isset($_post['submit'])){ $number=$_post['number']; $selected_choice = $_post['choice']; $next=$number+1; /* * total questions */ $query="select* questions"; $results= mysqli_query($con,$query); $total=$results->num_rows; /* * correct choice */ $query = "select* `choices` question_number = $number , is_correct=1"; //get result $results= mysqli_query($con,$query); //get row $row=$results->fetch_assoc(); //set correct choices $correct_choice=$row['id']; //compare choice made correct choice if($correct_choice == $selected_choice){ $_session['score']++; } //check if last question if($number == $total){ header("location: final.php"); exit(); } else { header("location: question.php?n=".$next); } }
just tried something:
if($correct_choice == $selected_choice){ echo "same"; } else{ echo "not same"; }
even though $correct_choice
, $selected_choice
both equals 1, code still returns "not same"?
you opening , closing lot of php tags - unnecessarily. if have error reporting on, , being displayed on screen, saying 'headers sent'. that, issue.
since process.php file php - open <?php
tag once - , don't bother close @ end - unless explicitly outputting (which bad practice anyway larger program, better use small framework, , separate templates).
Comments
Post a Comment