Using php and mysql .. I want to execute two sql statements ... the first one to create a purchase order (PO) in the purchaseorders table ... the second to use that purchase order id to create multiple inserts into a 'podetails' table to capture the items that are contained with that PO. I cannot get multiple sql statements executed to do this. I might be approaching this completely wrong ... so will appreciate any help I can get.
If I only have the first part of the code to create the PO record - that works ... but when I add the second part to insert each record in the podetails table - nothing works. It no longer creates a PO record in the 'purchaseorders' table.
<?php
// establish DB connection (this includes creates $conn variable)
include_once 'dbh.inc.php';
session_start();
$userId = $_SESSION['u_Id'];
$supplierId = $_POST['supplierId'];
$order_items = $_POST['orderitems'];
// create PO record
$sql = "INSERT INTO purchaseorders (PODate, supplierId, userId)
VALUES (?,?,?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../place.php?error=sqlerror");
echo "SQl Connect error!";
}
else {
$date = date('Y-m-d H:i:s');
mysqli_stmt_bind_param($stmt, "sss", $date, $supplierId,
$userId);
mysqli_stmt_execute($stmt);
}
// // get last PO record id
$POId = mysqli_insert_id($conn);
// for each record in grid - create PO details record
foreach ($order_items as $item) {
$sql1 = "INSERT INTO podetails (PONumber, productId, poquantity,
pocostPrice, delivered, paidStatus) VALUES
(?,?,?,?,?,?);";
$stmt1 = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt1, $sql1)) {
echo "SQl Connect error!";
}
else {
mysqli_stmt_bind_param($stmt1, "ssssss", $POId,
$item['prodId'], $item['qty'], $item['costPrice'],
"false", "false");
mysqli_stmt_execute($stmt1);
echo "PO Placed: PO Number = ".$POId;
}
}
?>
I expected the output to be the PO number (after all podetail items records where created), but this code does not create any records.