duangou2046 2011-08-30 18:28
浏览 74
已采纳

为什么我的if语句会忽略我的条件?

I'm programming a random event system that happens to users when logged in and I put the below piece of code into my include file.

$tehchance = mt_rand(1,15);
if ($tehchance == "1"){
$thewin = 10;
mysql_query("UPDATE members SET Points = Points + $thewin WHERE Handle = '$members[Handle]'");
}

I also have this for another event:

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]'");
}

The code will work but sometimes when $tehchance is equal to something else other than 1 or 2, it'll just ignore my conditions and update the members table without satisfying the if statement. From testing, it'll randomly add points or subtract points. I printed the random number from $tehchance and it still adds points even when it isn't equal to 1 or 2. Then sometimes it doesn't do anything to the members table. Really confused here.

Any ideas?

  • 写回答

2条回答 默认 最新

  • dongtaogu8510 2011-08-30 18:43
    关注

    Try using an if-then-else and debug that.

    $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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料