我不能一次为多个if语句添加点数吗?

时间:2011-07-17 17:14:14

标签: php mysql

好吧,我有一个成就系统,除了正确累加积分外,它的效果很好。以下是在满足条件时更新到数据库的示例:

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。因此,我的主要包含文件中有35组这两个if语句。我的会员表中的测试用户个人资料已经满足35个成就中的19个的条件。除了添加Points之外,数据库中的所有内容都会正确更新。积分总数应该超过1000点左右,但它总是给我150分。我不确定我做错了什么。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我不能具体说明为什么它没有添加点,在将数据库内容移动到它自己的函数之后我会说这是因为你没有增加并存储基点值$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());
}