在while循环中查询

时间:2013-01-31 15:32:43

标签: php mysql sql loops while-loop

这个代码有什么问题吗?

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.'');
}

这实际上是整个代码。 :

1 个答案:

答案 0 :(得分:1)

  

我是否需要使用return函数让它继续循环

您不需要返回以继续循环。如果要从当前函数返回并返回,请使用return

  

如果在函数内调用,则立即返回语句   结束当前函数的执行,并将其参数作为   函数调用的值。 return也将结束执行   一个eval()语句或脚本文件。

PHP&amp;查询

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循环只执行一次。