我正在进行在线测验,在这个测验中,一次询问一个问题。如果答案是正确的,我想将这一点添加到所获得的总分中。我希望$ _SESSION显示到目前为止用户获得了多少积分。我到目前为止的代码是:
$sql = "SELECT * FROM quiz_questions WHERE Question='$question' AND Answer='$answer'";
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
if ($num_rows != 0){
$score++;
$total_score += $score;
session_start();
$_SESSION['score'] = $total_score;
} else {
echo "Incorrect";
}
}
我已经将$得分等于0,从代码的开头开始。提前感谢您的帮助!
答案 0 :(得分:0)
session_start(); // should be at the beginning of your script(s);
if (!isset($_SESSION['score'])) { $_SESSION['score'] = 0; }
$sql = "SELECT * FROM quiz_questions WHERE Question='$question' AND Answer='$answer'";
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
if ($num_rows != 0){
// $row = fetch row to get the actual score for this question, if answered right?
$row = mysql_fetch_row($result);
//$score++; ??????
//$total_score += $score;
$_SESSION['score'] += $row['score_index'];
//$_SESSION['score']++; // or just this, dunno
echo 'Your current score is: '.$_SESSION['score'];
}
else {
echo "Incorrect";
}