简体   繁体   English

if/elseif/else 语句不能正常工作

[英]if/elseif/else statement not working as it should

As the title says I have a problem running an if/elseif/else statement query, long story short it's not entering the condition correctly.正如标题所说,我在运行 if/elseif/else 语句查询时遇到问题,长话短说,它没有正确输入条件。

Here is the PHP code这是 PHP 代码

<?php 

$time = $_SERVER['REQUEST_TIME'];
$query0 = "SELECT lastco FROM customers";
$query_run0 = mysqli_query($conn, $query0);

if(mysqli_num_rows($query_run0) > 0)
{
    while($row = mysqli_fetch_assoc($query_run0))
    {
        if($row['lastco'] < $time-300)
        {
            if($row['lastco'] < $time-2000000)
            {
                $query1 = "UPDATE customers SET customer_status='Not Registered' WHERE lastco < $time-2000000";
                $query_run1 = mysqli_query($conn, $query1);
            }
            else
            {
                $query2 = "UPDATE customers SET customer_status='Offline' WHERE lastco < $time-300";
                $query_run2 = mysqli_query($conn, $query2);
            }
            
        }
        elseif($row['lastco'] > $time-300)
        {
            $query3 = "UPDATE customers SET customer_status='Online' WHERE lastco > $time-300";
            $query_run3 = mysqli_query($conn, $query3);
        }
    }
}?>

So the problem is that instead of modifying the customer_status row to 'Not Registered' it will just modify it to 'Offline', and never edit the row to 'Not Registered' even if the condition is fulfilled so what can I do to make that statement work that way?所以问题是,不是将 customer_status 行修改为“未注册”,而是将其修改为“离线”,即使满足条件也永远不会将该行编辑为“未注册”,所以我该怎么做才能做到这一点声明工作方式?

Your if conditions works just fine.你的if条件工作得很好。 Instead of defaulting to the assumption that if is somehow broken, examine your logic.不要默认假设if以某种方式被破坏,而是检查您的逻辑。 Start by putting that logic into simple descriptions:首先将该逻辑放入简单的描述中:

For *every record*, repeat:
  If some value is below 300
    If some value is below 2000000
      Update *all values* that are below 2000000
    Else
      Update *all values* that are below 300
  Else, if some value is above 300
    Update *all values* that are above 300

So... If each iteration of the loop is meant to update all values , then why do you need to repeat that operation in a loop at all?所以......如果循环的每次迭代都意味着更新所有值,那么为什么你需要在循环中重复该操作呢?

And, more to the point here... When you Update *all values* that are below 300 , why do you expect that wouldn't also Update *all values* that are below 2000000 ?而且,更重要的是……当您Update *all values* that are below 300时,您为什么期望它不会也Update *all values* that are below 2000000 After all, any number which is lower than 300 is also lower than 2000000.毕竟,任何低于 300 的数字低于 2000000。

Scrap the loop and all the if/else blocks entirely.完全废弃循环和所有 if/else 块。 Just perform the updates you want to perform:只需执行您要执行的更新:

Update *all values* that are below 2000000
Update *all values* that are below 300
Update *all values* that are above 300

Forget the SELECT , forget the loop, just execute your three UPDATE statements to update your data.忘记SELECT ,忘记循环,只需执行三个UPDATE语句来更新数据。 (You could do it in one UPDATE statement, but for clarity let's just start with removing all the cruft of that loop.) (您可以在一个UPDATE语句中完成它,但为了清楚起见,让我们从删除该循环的所有内容开始。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM