gpt4 book ai didi

php - while 循环内查询

转载 作者:行者123 更新时间:2023-11-29 14:04:08 25 4
gpt4 key购买 nike

这段代码有什么问题吗?

while($qtytoAdd > 0) {
if(($remBalance - $qtytoAdd) >= 0) {
mysql_query('UPDATE `estimates` SET `qty_rec` = `qty_rec` + "'.$qtytoAdd.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `est_id` = "'.$balid.'"');
mysql_query('UPDATE `requestdetails` SET `current_rec_qty` = `current_rec_qty` + "'.$qtytoAdd.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `req_id` = "'.$currid.'"');
} else {
mysql_query('UPDATE `estimates` SET `qty_rec` = "'.$remBalance.'" + "'.$currRec.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `est_id` = "'.$balid.'"');
mysql_query('UPDATE `requestdetails` SET `current_rec_qty` = "'.$remBalance.'" + "'.$currRec.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `req_id` = "'.$currid.'"');
$qtytoAdd = $qtytoAdd - $remBalance;
}
return $qtytoAdd;
}

这两个条件似乎都有效,但我为 $qtytoAdd 分配新值的部分却无效。我是否需要使用 return 函数让它继续循环。我是新手。请帮忙。

foreach($mat_desc as $mat_key => $materials){
$qtytoAdd = $rec_qty[$mat_key];
$remBalance = mysql_result(mysql_query('SELECT `est_qty` - `qty_rec` as balance FROM `estimates` WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `qty_rec` <> `est_qty` ORDER BY `item_id` ASC'), 0);
$balid = mysql_result(mysql_query('SELECT `est_id` FROM `estimates` WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `qty_rec` <> `est_qty` ORDER BY `item_id` ASC'), 0);
$currRec = mysql_result(mysql_query('SELECT `qty_rec` FROM `estimates` WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `qty_rec` <> `est_qty`'), 0);
$currid = mysql_result(mysql_query('SELECT `req_id` FROM `requestdetails` JOIN `request` USING(`req_id`) WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `req_qty` <> `current_rec_qty` ORDER BY `req_id` ASC'), 0);

mysql_query('INSERT INTO `receivedetails` (`rec_id`,`mat_id`,`rec_qty`) VALUES ('.$rec_id.','.mat_id_from_mat_desc($materials).','.$qtytoAdd.')');

while($qtytoAdd > 0) {
if(($remBalance - $qtytoAdd) >= 0) {
mysql_query('UPDATE `estimates` SET `qty_rec` = `qty_rec` + "'.$qtytoAdd.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `est_id` = "'.$balid.'"');
mysql_query('UPDATE `requestdetails` SET `current_rec_qty` = `current_rec_qty` + "'.$qtytoAdd.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `req_id` = "'.$currid.'"');
$qtytoAdd = 0;
} else {
mysql_query('UPDATE `estimates` SET `qty_rec` = "'.$remBalance.'" + "'.$currRec.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `est_id` = "'.$balid.'"');
mysql_query('UPDATE `requestdetails` SET `current_rec_qty` = "'.$remBalance.'" + "'.$currRec.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `req_id` = "'.$currid.'"');
$qtytoAdd = $qtytoAdd - $remBalance;
}
}

mysql_query('UPDATE `estimates` SET `qty_onhand` = `qty_rec` - `qty_rel` WHERE `proj_id` = '.$proj_id.' AND `qty_rec` <> `est_qty` AND `mat_id` = '.mat_id_from_mat_desc($materials).' AND `est_id` = '.$balid.'');
}

这实际上是整个代码。 :]

最佳答案

do I need to use the return function to let it continue looping

您不需要返回即可继续循环。您使用return当你想从当前函数返回并返回时。

If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return will also end the execution of an eval() statement or script file.

PHP 和查询

while($qtytoAdd > 0) {
if(($remBalance - $qtytoAdd) >= 0) {
mysql_query('UPDATE `estimates` SET `qty_rec` = `qty_rec` + "'.$qtytoAdd.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `est_id` = "'.$balid.'"');
mysql_query('UPDATE `requestdetails` SET `current_rec_qty` = `current_rec_qty` + "'.$qtytoAdd.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `req_id` = "'.$currid.'"');
$qtytoAdd = $qtytoAdd - $remBalance; // this part does not exist in your code.
^^^^^^^^^^^^^^^^^^^^^^-------- depends on your requirement. I added the same line.
} else {
mysql_query('UPDATE `estimates` SET `qty_rec` = "'.$remBalance.'" + "'.$currRec.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `est_id` = "'.$balid.'"');
mysql_query('UPDATE `requestdetails` SET `current_rec_qty` = "'.$remBalance.'" + "'.$currRec.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `req_id` = "'.$currid.'"');
$qtytoAdd = $qtytoAdd - $remBalance;
}
}

哪里出了问题

根据您的代码,如果第一个 if 为 true,则您不会对 $qtytoAdd 进行更改。另外,您从函数中返回。所以 while 循环只会执行一次。

关于php - while 循环内查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14629337/

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