![](/img/trans.png)
[英]How can I avoid the use of this nested while loop with a SQL query execution inside it?
[英]How can I avoid multiple deduction from stocks in my SQL db in php while loop?
當我有很多人在我的網站上下訂單后嘗試結帳時,我遇到了問題。 我使用while循環來確保訂購的數量從我的SQL數據庫中的可用數量中扣除,但不是按計划扣除,而是扣除多余的。
我做錯了嗎?
<?php
//Database connection
include("db/config.php");
//Session to get user id
$user_id=$_SESSION['id'];
$statuss='added';
$value2='';
//Query to fetch last inserted invoice number and create a new invoice number
$query = "SELECT invoice_no from invoice order by invoice_no DESC LIMIT 1";
$stmt = $conn->query($query);
if(mysqli_num_rows($stmt) > 0) {
if ($row = mysqli_fetch_assoc($stmt)) {
$value2 = $row['invoice_no'];
$value2 = substr($value2, 10, 13);//separating numeric part
$value2 = $value2 + 1;//Incrementing numeric part
$value2 = "ARW/19-20/" . sprintf('%03s', $value2);//concatenating incremented value
$value = $value2;
}
}
else {
$value2 = "ARW/19-20/001";
$value = $value2;
}
// echo $value;
if(isset($_POST['check_out'])){
//Main code
//Get all input values
$name=mysqli_real_escape_string($conn,$_POST['name']);
$phone=mysqli_real_escape_string($conn,$_POST['phone']);
$address=mysqli_real_escape_string($conn,$_POST['address']);
$payment_method=mysqli_real_escape_string($conn,$_POST['payment_mtd']);
$added='added';
//Get session name $user_name=$_SESSION['name'];
//Query to update order operation table
$sql="UPDATE order_operation SET name='$name', user_name='$user_name', phone='$phone', address='$address',payment='$payment_method' WHERE user_id=$user_id AND status='$added'";
if($conn->query($sql) === TRUE){
//Declare variables
$before='added';
$status='completed';
//Update order operation with created invoice number
$sql="UPDATE order_operation SET status=?, invoice_no=? WHERE user_id='$user_id' AND status='$before'";
$stmt=$conn->prepare($sql);
$stmt->bind_param('ss',$status,$value);
if($stmt->execute()){
$sql="INSERT INTO invoice(invoice_no,user_id) VALUES('$value','$user_id')";
$conn->query($sql);
//Deduct from stock
$sql = "SELECT * FROM `order_operation` WHERE invoice_no='$value'";
$sql_run = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($sql_run)) {
//Deduct from stock since the order id's meet the requirements $deduct=$row['stock_quantity'] - $row['quantity'];
$stock_id=$row['stock_id'];
$connector= "UPDATE stocks SET quantity = '$deduct' WHERE id = $stock_id" ;
if($conn->query($connector) === TRUE){
$_SESSION['customer_name']=$name;
$_SESSION['phone']=$phone;
$_SESSION['address']=$address;
$_SESSION['payment']=$payment_method;
$_SESSION['invoice_no']=$row['invoice_no'];
header('location:order_invoice.php');
}else{
echo $conn->error;
}
}
//End of deduct from stock
$conn->close();
}else{
die($conn->error);
}
}else{
die($conn->error);
}
$stmt->close();
$conn->close();
}
?>
SELECT
后跟取決於它的UPDATE
始終是競爭條件。 使用來自以下的多表更新:
UPDATE order_operation oo, stocks s
SET s.quantity = s.quantity - MIN(oo.stock_quantity, s.quantity),
oo.stock_quantity = MIN(oo.stock_quantity, s.quantity)
WHERE s.id = oo.stock_id
AND oo.invoice_id = X
數量的附加條件可確保庫存水平和訂單不超過可用庫存,但您可能希望以另一種方式執行此操作以確保訂單對減少感到滿意。
如果您發現自己在循環中執行 SQL ,那么數據庫語法很有可能會做得更好。
還要查找准備好的語句並在mysqli_real_escape_string
變體上使用這些語句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.