好吧,我有一个成就系统,它工作得很好,除了正确地加上分数。以下是满足条件时更新数据库的示例:

if($members['Active']==1){
    $Achievement1=mysql_query("SELECT * FROM ".
                                  "myachievements WHERE ".
                              "Handle='$members[Handle]' and AchievementId='1'");
    $Achievement2=mysql_num_rows($Achievement1);
    $tehdate=date('Y-m-d');
    $Points = '50';
    $newscore=$members['Points']+$Points;
    if ($Achievement2 == 0) {
        mysql_query("INSERT INTO `myachievements`
                         ( `Handle` , `AchievementId` , `Date` ) VALUES
                            ('$members[Handle]', '1' , '$tehdate')"
                    )or die(mysql_error());

        mysql_query ("UPDATE members SET Achievement = 'Y' WHERE ".
                         "Handle = '$members[Handle]'")or die(mysql_error());
        mysql_query ("UPDATE members SET Points = '$newscore' WHERE ".
                         "Handle = '$members[Handle]'")or die(mysql_error());
    }

}

上面显示的成就只是为了激活一个帐户。我现在有大约35个成就,每个都有一个不同的ID,所以在我的主include文件中有35组这两个if语句。我的成员表中的测试用户配置文件已经满足35个成就中19个的条件。除了添加点之外,数据库中的所有内容都将正确更新。总分应该在1000分左右,但它总是给我150分。我不知道我做错了什么。任何帮助都将不胜感激。

最佳答案

我不能具体说明为什么它不添加点,在将数据库内容移动到它自己的函数中之后,我会说这是因为您没有增加基点值并将其存储在$members['Points']

if($members['Active']==1)
{
  $Points = '50';
  $newscore = $members['Points'] + $Points;

  if (0 == achievement_get_count($members['Handle'], 1))
  {
    achievement_add($members['Handle'], 1, date('Y-m-d'));
    achievement_set_points($members['Handle'], $newscore);
  }
}

/** achivement mysql functions */

function achievement_get_count($memberHandle, $achievementId) {
  $Achievement1=mysql_query("SELECT * FROM myachievements WHERE Handle='$memberHandle' and AchievementId='$achievementId'");
  return mysql_num_rows($Achievement1);
}

function achievement_add($memberHandle, $achievementId, $date) {
  mysql_query("INSERT INTO `myachievements` ( `Handle` , `AchievementId` , `Date` )
    VALUES (
      '$memberHandle', '$achievementId' , '$date' )")
  or die(mysql_error());
}

function achievement_set_points($memberHandle, $points) {
    mysql_query ("UPDATE members SET Achievement = 'Y', Points = '$points' WHERE Handle = '$memberHandle'")or die(mysql_error());
}

关于php - 我不能一次为多个if语句加点吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6725439/

10-12 12:42