At first glance I suspected that a redirect may be happening after the first insert query and before the update query runs. I ran your query through ChatGPT and this is what it came back with:
It seems like you're attempting to insert records into one table (orders) and update records in another table (cart) upon form submission. However, it appears that there might be an issue with your logic. Let's go through your code and see if we can identify and fix the problem:
Insert Query: You're inserting a record into the orders table. This part seems fine.
Update Query: You're trying to update records in the cart table. You're looping through each cartID from the submitted form. This part also seems fine.
It's possible that the redirection is happening prematurely, or there might be some issue with the redirect logic.
Let's make some adjustments to your code to improve readability and address the potential issue:
<?php
if (isset($_POST["submit"]) || isset($_POST["submit_x"])) {
if ($customers->getColumnVal("delivery_allowed") == 'N') {
$type = 'Pickup';
} else {
$type = htmlspecialchars($_POST['type']);
}
$delivery_date = htmlspecialchars($_POST['delivery_date']);
$InsertQuery = new WA_MySQLi_Query($old_mule);
$InsertQuery->Action = "insert";
$InsertQuery->Table = "orders";
$InsertQuery->bindColumn("customerID", "i", $customerID, "WA_DEFAULT");
$InsertQuery->bindColumn("date_submitted", "s", $today, "WA_DEFAULT");
$InsertQuery->bindColumn("type", "s", $type, "WA_DEFAULT");
$InsertQuery->bindColumn("delivery_date", "t", $delivery_date, "WA_DEFAULT");
$InsertQuery->saveInSession("orderID");
$InsertQuery->execute();
$orderID = $_SESSION['orderID'];
$status = 'Order Placed';
foreach($_POST['cartID'] as $cartID2) {
$UpdateQuery = new WA_MySQLi_Query($old_mule);
$UpdateQuery->Action = "update";
$UpdateQuery->Table = "cart";
$UpdateQuery->bindColumn("orderID", "i", $orderID, "WA_DEFAULT");
$UpdateQuery->bindColumn("status", "s", $status, "WA_DEFAULT");
$UpdateQuery->addFilter("cartID", "=", "i", $cartID2);
$UpdateQuery->execute();
}
// Redirect after processing all updates
$UpdateGoTo = ""; // Your redirection URL
if (function_exists("rel2abs")) {
$UpdateGoTo = $UpdateGoTo ? rel2abs($UpdateGoTo, dirname(__FILE__)) : "";
}
$UpdateQuery->redirect($UpdateGoTo);
}
?>
Adjustments made:
Moved the $orderID and $status assignment inside the main IF block to ensure they are set correctly.
Removed unnecessary isset() checks in the bindColumn calls.
Removed redirection after each update in the loop and placed it after the loop to redirect only once after all updates are done.
Added comments for clarity.
Make sure to replace Your redirection URL with the actual URL you want to redirect the user to after processing the updates. Additionally, ensure that error reporting is enabled to catch any potential errors that might be occurring during execution.