Im working on invertory management system and I'm not sure if this is the best way to substract product quantity.
This query (function) will be executed when the orders will be exported to courier services.
First of all I get the product details. In this example the quantity and SKU code.
//// GET PRODUCT DETAILS
$stmtgetproductdata = $connpdo->prepare("SELECT products.sku, products.productqty FROM `orders` inner join orderitems on orderitems.orderid = orders.orderid inner join products on products.productid = orderitems.productid WHERE orders.customerid =:id");
$stmtgetproductdata->bindValue(":id", 'ORDERID');
$stmtgetproductdata->execute();
$productswithdetails = array();
In while loop I put these informations from product to array.
while ($rowproduct = $stmtgetproductdata->fetch()) {
$productQTY = $rowproduct['productqty'];
$productSKU = $rowproduct['sku'];
$productswithdetails[] = array(
"sku" => $productSKU,
"qty" => $productQTY
);
}
And in end of the code (if everything was sucessfull) I run updatequery() function:
function updatequery($connpdo, $productarray){
foreach ($productarray as $product){
$stmtupdateQTY = $connpdo->prepare("UPDATE invertory SET qty = qty - :qty WHERE sku = :productsku;");
$stmtupdateQTY->execute([":qty"=>$product["qty"],
":productsku"=>$product["sku"]]);
}
}
I need foreach is because there can be more then 1 product / order.
What do you think? Is this a safe way to update the inventory table? Thank you!