[英]How to execute multiple SQL query to different table?
我正在嘗試對不同的表運行這兩個查詢。
當插入tbl_invoice
, invoice_id
是從第一個查詢創建的(第一個有效)但是當我對tbl_invoice_details
進行另一個查詢時,我認為它實際上並沒有通過之前的invoice_id
值,嘗試使用if($invoice_id != null){...}
但此查詢似乎不適用於tbl_invoice_details
。
任何幫助將不勝感激,謝謝。
include_once 'connectdb.php';
$id = $_GET['id'];
$select = $pdo->prepare("SELECT * FROM tbl_product WHERE barcode=$id");
$select->execute();
$row = $select->fetch(PDO::FETCH_OBJ);
// FOR TBL_INVOICE
$variables
...
$insert = $pdo->prepare("INSERT INTO tbl_invoice(customer_name,order_date,subtotal,tax,discount,total,paid,due,payment_type,profit) values(:cust,:orderdate,:stotal,:tax,:disc,:total,:paid,:due,:ptype,:profit)");
$insert->bindParam(':cust',$customer_name);
$insert->bindParam(':orderdate',$order_date);
$insert->bindParam(':stotal',$subtotal);
$insert->bindParam(':tax',$tax);
$insert->bindParam(':disc',$discount);
$insert->bindParam(':total',$total);
$insert->bindParam(':paid',$paid);
$insert->bindParam(':due',$due);
$insert->bindParam(':ptype',$payment_type);
$insert->bindParam(':profit',$profit);
$insert->execute();
// FOR TBL_INVOICE_DETAILS
$invoice_id = $pdo->lastInsertId();
if($invoice_id != null){
$rem_qty = $stock - $qty;
$update = $pdo->prepare("UPDATE tbl_product SET pstock='$rem_qty' WHERE barcode='".$id."'");
$update->execute();
$insert = $pdo->prepare("INSERT INTO tbl_invoice_details(invoice_id,product_id,product_name,qty,price,order_date,values(:invid,:pid,me,:qty,:price,:orderdate,:profit)");
$insert->bindParam(':invid',$invoice_id);
$insert->bindParam(':pid',$pid);
$insert->bindParam(':pname',$productname);
$insert->bindParam(':qty',$qty);
$insert->bindParam(':price',$total);
$insert->bindParam(':orderdate',$order_date);
$insert->bindParam(':profit',$profit);
$insert->execute();
}
?>```
啟用 PDO 錯誤報告:
include_once 'connectdb.php';
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$id = $_GET['id'];
當發生 SQL 錯誤時,它將作為異常拋出。 如果我們不啟用 PDO 錯誤報告,那么代碼需要檢查每個 SQL 調用的返回值,以查看是否發生了錯誤。
這里的問題不是“如何將多個 SQL 查詢發送到不同的表”。 真正的問題是“我如何查看發生了什么 SQL 錯誤”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.