I'm attempting to run both query to different table.
As tbl_invoice
are inserted, invoice_id
is created from first query (first one works) but when I put another query for tbl_invoice_details
I think it doesn't actually pass the previous invoice_id
value, tried with if($invoice_id != null){...}
but this query doesn't seem to work for tbl_invoice_details
.
Any help would be appreciated, thanks.
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();
}
?>```