【问题标题】:How do I do a Foreach loop and Mysql with a specific condition?如何在特定条件下执行 Foreach 循环和 Mysql?
【发布时间】:2021-12-11 06:44:08
【问题描述】:

我有点困惑,我试图在网上查找,但找不到任何适合我需要的答案。 我遇到了 PHP / MYSQL 的问题

我有一个用户列表,每个用户当前都活跃。

我想为会员已过期的用户创建一个 MYSQL UPDATE (active = 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 &lt; now()?????

标签: php mysql for-loop foreach


【解决方案1】:

保持简单,并在一个命令中完成。

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

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

【讨论】:

    猜你喜欢
    • 2021-01-27
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    相关资源
    最近更新 更多