简体   繁体   English

如果while循环中的if语句不更新数据库中的所有用户

[英]If statement in while loop not updating all users in database

Im trying to update all users in the database to get +5 turns every time this script is run. 我试图每次运行此脚本时更新数据库中的所有用户以使其旋转+5。

But it updates only the first user in the database untill it has full turns and then it updates the next user. 但是它只更新数据库中的第一个用户,直到它轮流进行,然后再更新下一个用户。

What do i have to do to add 5 turns to all users but dont add any if they have 100/100 turns? 我该怎么做才能向所有用户添加5转,但是如果他们有100/100转则不添加任何转弯?

<?php

include ("functions/db_connect.php");

$get_users = mysqli_query($db, "SELECT * FROM `members`") or die(mysql_error());
while($user = mysqli_fetch_assoc($get_users)){

    if ($user['current_turns'] < $user['max_turns']) {
        $update = mysqli_query($db, "UPDATE `members` SET
                                `current_turns`=`current_turns`+'5' WHERE `id`='".$user['id']."'") or die(mysql_error());
        header("Location: users.php");
        exit();

    }
}

?>

Try using a query like this to update all the members at the same time instead of selecting them all and looping through updating them one by one. 尝试使用这样的查询来同时更新所有成员,而不是全部选择它们并一遍又一遍地更新它们。

UPDATE members 
SET current_turns = LEAST(max_turns, current_turns + 5)
WHERE current_turns < max_turns

Using the LEAST function will allow the current_turns to be incremented up to max_turns without going over, as I assume this would be undesirable, but it does depend on neither column being null. 使用最小二乘功能将允许current_turns要递增高达max_turns,但是不能超出,因为我认为这是不可取的,但它不依赖于没有列被空。

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

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