【发布时间】:2014-12-16 00:14:11
【问题描述】:
我正在编写产品库存脚本。我有一个 MySQL 表“products_stock”:
id zid pid amount
1 123 321 1
2 124 321 5
4 124 566 7
3 125 321 10
因此,库存中的产品 id = 321 的总量为 16。有人用 pid=321 和 qty = 7 下订单。 我需要一些 php 函数,它将从第一个 zid 开始的列 amount 中减去 7,并更新表 products_stock 中的记录,以便它照看是这样的:
id zid pid amount
1 123 321 0
2 124 321 0
4 124 566 7
3 125 321 9
我试图做的事情:
function mysql_fields_minus_value ($pid, $amount) {
$q_select_stock_data = "SELECT * FROM `products_stock` WHERE `pid` = '".$pid."'";
$r_select_stock_data = mysql_query($q_select_stock_data);
if (mysql_num_rows($r_select_stock_data)>0) { // First we check if there is such product in stock
// Then we go through all records which have that pid
while ($a_select_stock_data=mysql_fetch_array($r_select_stock_data)) {
// echo "<p>".$a_select_stock_data['stock']."</p>";
$product_pid = $a_select_stock_data['sku'];
$product_qty_order = 7; // qty of pid in order
// than we check if we have enough qtys of product in stock
if ($a_select_stock_data['amount'] - $amount)>=0) { // ok- enough
从这一点开始,我被困住了。
感谢您的回答!
【问题讨论】:
-
您能分享您尝试过的内容还是希望我们为您写出来?
-
如果您遇到困难,请尝试编写您的函数并在此处发布代码。你的问题对于stackoverflow来说太宽泛了。
-
我必须同意每个人的观点,这是一个糟糕的问题,你应该给社区更多的帮助来帮助你
-
抱歉问题很糟糕,我已经添加了我想要做的事情
-
拜托,don't use
mysql_*functions,它们不再维护,而是officially deprecated。改为了解prepared statements,并使用PDO 或MySQLi。 This article 将帮助您做出决定。
标签: php mysql function math operations