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



正如标题所说,我在运行if/elseif/else语句查询时遇到问题,长话短说,它没有正确输入条件。

以下是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);
}
}
}?>

因此,问题是,它不会将customer_status行修改为"Not Registered",而是将其修改为"Offline",并且即使满足条件,也不会将该行编辑为"Not Registered",那么我该怎么做才能使该语句以这种方式工作呢?

您的if条件运行良好。与其默认if在某种程度上被破坏,不如检查您的逻辑。首先把这个逻辑放在简单的描述中:

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

所以。。。如果循环的每次迭代都是为了更新所有值,那么为什么需要在循环中重复该操作呢?

更重要的是。。。当你Update *all values* that are below 300时,为什么你认为它不会也是Update *all values* that are below 2000000?毕竟,任何低于300的数字都是,也低于2000000。

完全废弃循环和所有if/else块。只需执行您想要执行的更新:

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

忘记SELECT,忘记循环,只需执行三条UPDATE语句即可更新数据。(您可以在一个UPDATE语句中完成,但为了清楚起见,让我们从删除该循环的所有关键开始。(

最新更新