gpt4 book ai didi

php - 使用变量更新表中的数字并更新变量

转载 作者:行者123 更新时间:2023-11-30 23:14:20 25 4
gpt4 key购买 nike

从下表可以看出,id为1的用户一共获得了10分
我想要做的是从表单 $edit_points 提交一个数字来循环并从用户 1 中减去
这个数字可以是任何东西,例如:

$edit_points = 6
将从第一条记录中减去 4
然后移动到下一个只减去余数2
然后跳出循环

UserID  | TotalPoints      
-------------------
1 | 4
1 | 4
1 | 2
4 | 3
3 | 3
5 | 4

这就是我目前所得到的

$pointLeft = $edit_points;//sent from form    
while($point = mysqli_fetch_array($output)){

if($pointLeft > $point["TotalPoints"]){//if pointsLeft more than this record
$remainder = $pointLeft-$point["TotalPoints"];//get remainder
$query2 = "UPDATE pointstable SET TotalPoints=0 WHERE UserID=".$UserID;
$output2 = $mysqli->query($query2);

}elseif($pointLeft < $point["TotalPoints"]){

$remainder = $point["TotalPoints"] - $pointLeft;
$query2 = "UPDATE pointstable SET TotalPoints=".$remainder." WHERE UserID=".$UserID;
$output2 = $mysqli->query($query2);

}

$pointLeft = $remainder;

}

它在一定程度上起作用,似乎用余数保持的相同最后一个值填充所有记录我试过 break 并将 if ($pointLeft<=0) 放在第一个 if
之上但没有一个有效。
有帮助吗?或者更好的方法最好使用 php

最佳答案

感谢马蒂
修改后的代码
有一个我现在使用的唯一 ID PointID

$poinstLeft = $edit_points; //sent from form

while($point = mysqli_fetch_array($output)){
//if pointsLeft more than this record
if($poinstLeft >= $point["TotalPoints"]){
//get remainder
echo $poinstLeft." ";
$remainder = $poinstLeft-$point["TotalPoints"];
$query2 = "UPDATE pointstable SET TotalPoints=0 WHERE UserID=".$UserID." AND PointID=".$point["PointID"];

$output2 = $mysqli->query($query2);
// Calculate new pointsleft
$poinstLeft = $remainder;

} elseif ($poinstLeft < $point["TotalPoints"]){
$remainder = $point["TotalPoints"] - $poinstLeft;
echo $poinstLeft." ";
$query2 = "UPDATE pointstable SET TotalPoints=".$remainder." WHERE UserID=".$UserID." AND PointID=".$point["PointID"] ;
$output2 = $mysqli->query($query2);
// Here, your remainder is different from the upper half of your if-function!
// So, you don't have to recalculate pointsleft... It's just:
$poinstLeft = 0;

// Break the while loop:
break 2;
}


}

关于php - 使用变量更新表中的数字并更新变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18620025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com