I am writing products stock script. I have a MySQL table "products_stock":
id   zid   pid  amount
1    123   321   1
2    124   321   5
4    124   566   7
3    125   321   10
So, total amount of product id = 321 in stock is 16. Somebody makes order with pid=321 and qty = 7. I need some php-function, which will minus 7 from column amount starting from the first zid, and update records in table products_stock so that it lookes after like this:
id   zid   pid  amount
1    123   321   0
2    124   321   0
4    124   566   7
3    125   321   9
What I have tried to do:
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
From this point I was stucked.
Thank you for answers!
 
    