如何在刷新页面时阻止计数器增加

时间:2011-07-29 12:48:27

标签: php

这是我的代码:

$score=0;
$sql10=mysql_query("select * from image WHERE fromUser='".$_SESSION['email']."' order by id desc");
$score1=mysql_fetch_array($sql10);
$answer=$score1['Correct_answer'];
echo $answer;

$sql13=mysql_query("select * from image WHERE toUser='".$_SESSION['email']."' order by id desc");
$score2=mysql_fetch_array($sql13);
$answer2=$score2['User_answer2'];
echo $answer2;


strcasecmp($answer,$answer2);
if(strcasemp)

echo $score +=1;

你可以看到我正在比较答案,如果两者相等,我希望得分增加1。但代码没有任何提示???

1 个答案:

答案 0 :(得分:1)

看起来您没有使用比较来增加分数。 if (strcasecmp)行没有做任何事情。您正在执行上面的strcasecmp($answer,$answer2),但不存储或使用比较结果。此外,strcasecmp()返回0表示相等。相反,尝试:

if(strcasecmp($answer,$answer2) === 0) {
  echo $score +=1;

}

为了防止每次刷新页面时增加此值,您需要在$_SESSION中存储已经处理过的一些指示。

session_start();

// Check if it hasn't already been answered.
if (!isset($_SESSION['answered']))
{
    // Do all your queries to check the answers.
    // etc...
    // etc...

    if(strcasecmp($answer,$answer2) === 0) {
      echo $score +=1;

      // Store an indicator in $_SESSION so this doesn't get processed again
      $_SESSION['answered'] = TRUE;
    }
}