测验样本中的会话条件

时间:2017-07-24 12:27:49

标签: php

以下是测验的process.php示例。一切正常,但如果用户对问题的回答是正确的,那么又回到问题并再次回答正确。分数增加更多,这是我的会话问题?

if(isset($_POST['submit'])){
//get values
$number = $_POST['number'];
$selected_choice = $_POST['choice'];
$next = $number + 1 ;
//get NOQ
$query_NOQ = "select * from questions";
$result_NOQ = $connection->query($query_NOQ) or die($connection->error.__LINE__);
$NOQ = $result_NOQ ->num_rows;

$query_co = "select * from choices where question_number = $number AND is_correct = 1";
$result_co = $connection->query($query_co) or die($connection->error.__LINE__);
$correct_row = $result_co->fetch_assoc();
$correct_answer = $correct_row['id'];

if($selected_choice == $correct_answer){
    $_SESSION['score']++;
}
if($number == $NOQ){
    header("Location:final.php");
}  else {
    header("Location:questions.php?n=".$next);
}

2 个答案:

答案 0 :(得分:0)

你会做这样的事情:

//Check if there is something for that question
if(!isset($SESSION['answered_' .$number])){
    if($selected_choice == $correct_answer){
        $_SESSION['score']++;
    }
    //Set something so if they come back nothing happens to score
    $SESSION['answered_' .$number] = TRUE;
} else {
    //Could put some logic here that they already answered this one?
}


if($number == $NOQ){
    header("Location:final.php");
}  else {
    header("Location:questions.php?n=".$next);
}

答案 1 :(得分:0)

您必须添加功能以检查用户是否回答了相同的问题?

  if(isset($_POST['submit'])){
    $number = $_POST['number'];
    $selected_choice = $_POST['choice'];
    $next = $number + 1 ;
    //get NOQ
    $query_NOQ = "select * from questions";
    $result_NOQ = $connection->query($query_NOQ) or die($connection->error.__LINE__);
    $NOQ = $result_NOQ ->num_rows;

    /*check here that user already answer this question from database (add userid in the DB or join with userid for identical purpose 
    of related user*/

    $check = sonefunc();

    if($check==true){
        $query_co = "select * from choices where question_number = $number AND is_correct = 1";
        $result_co = $connection->query($query_co) or die($connection->error.__LINE__);
        $correct_row = $result_co->fetch_assoc();
        $correct_answer = $correct_row['id'];

        if($selected_choice == $correct_answer){
            $_SESSION['score']++;
        }
        if($number == $NOQ){
            header("Location:final.php");
        }  else {
            header("Location:questions.php?n=".$next);
        }
    }else{
        echo "Already answered this question";
    }
 }