如何在特定条件下执行 Foreach 循环和 Mysql?

How do I do a Foreach loop and Mysql with a specific condition?

我有点困惑,我尝试在网上查找但找不到适合我需要的答案。 我遇到 PHP / MYSQL

的问题

我有一个 用户 的列表,每个用户目前都是 活跃的。

我想为会员资格已过期的用户创建 MYSQL 更新(活动 = 0)。

用户在创建帐户并拥有 1 年的会员资格时自动设置为 1。如果日期过去了。此用户将自动转到活动行 (0) 而不是 (1),并且该帐户将自动停用。

我想循环执行此操作,因为我们有数百名成员有一段时间没有连接,除非他们登录,否则他们的帐户不会被停用。

我知道我错过了一些东西(一个 foreach 或一个 for )但不知道如何表示它。

有人可以帮帮我吗?

非常感谢。

<?php include("config.php");


$date1 = date("d/m/Y"); // currently date of today
$bouclerequete = "SELECT * FROM users";  // connect all the users in once.
$resultboucle = $link->query($bouclerequete);


while($rowtable = mysqli_fetch_array($resultboucle, MYSQLI_ASSOC)){   // while fetch array with all the users


 $date2 = $rowtable["datefincontrat"]; // end of the contract

 if ($date1 > $date2){  // if the date of today is lower than the date of their membership. we don't do anything




 }
 else{  // Otherwise , the membership will return to 0 and the account will be desactivated. 

   $ids2 = $rowtable["id"];
   $sql5 = "UPDATE users SET Active='0' WHERE id=$ids2";

   if ($link->query($sql5) === TRUE) {

   } else {
     echo "Error updating record: " . $conn->error;
   }



 }

}


?>

保持简单,一条命令即可完成。

UPDATE users SET Active='0' WHERE datefincontrat < CURDATE();

不过,您的代码易受 攻击sql 注入,所以请继续阅读准备带参数的语句。参见:How can I prevent SQL injection in PHP?