我正在编写一个随机事件系统,当用户登录时会发生这种情况,并将下面的代码放入我的包含文件中。
$tehchance = mt_rand(1,15);
if ($tehchance == "1"){
$thewin = 10;
mysql_query("UPDATE members SET Points = Points + $thewin WHERE Handle = '$members[Handle]'");
}
我还有另一个活动:
if ($tehchance == "2"){
$thekhwin = 5;
$thexpwin = 10;
mysql_query("UPDATE members SET Points = Points - $thekhwin WHERE Handle = '$members[Handle]'");
mysql_query("UPDATE members SET XP = XP + $thexpwin WHERE Handle = '$members[Handle]'");
}
代码可以工作,但有时当$ tehchance等于1或2以外的其他东西时,它只会忽略我的条件并更新成员表而不满足if语句。从测试开始,它会随机添加点数或减去点数。我打印了来自$ tehchance的随机数,即使它不等于1或2,它仍然会添加点。然后有时它对成员表没有任何作用。这里真的很困惑。
有什么想法吗?
答案 0 :(得分:1)
尝试使用if-then-else并调试它。
$tehchance = mt_rand(1,15);
if ($tehchance === 1){
echo 'doing 1';
$thewin = 10;
mysql_query("UPDATE members SET Points = Points + $thewin WHERE Handle = '$members[Handle]'");
} else if ($tehchance === 2){
echo 'doing 2';
$thekhwin = 5;
$thexpwin = 10;
mysql_query("UPDATE members SET Points = Points - $thekhwin WHERE Handle = '$members[Handle]'");
mysql_query("UPDATE members SET XP = XP + $thexpwin WHERE Handle = '$members[Handle]'");
} else {
echo 'doing nothing';
}
答案 1 :(得分:0)
因为您要与字符串进行比较:“1”而不是数字1(不带引号)