close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Update Query with Loop

Thread began 4/08/2024 10:32 am by LWat85283715 | Last modified 4/10/2024 10:25 am by Mags | 30 views | 1 replies |

LWat85283715

Update Query with Loop

I am trying to update multiple recordsets on submit. The first insert works correctly but, nothing after that works. Thanks

Here is my code:

<?php
if (isset($_POST["submit"]) || isset($_POST["submit_x"])) {
if ($customers->getColumnVal("delivery_allowed") == 'N') {
$type = 'Pickup'; }
$delivery_date = htmlspecialchars($_POST['delivery_date']);
if ($customers->getColumnVal("delivery_allowed") == 'Y' or $customers->getColumnVal("delivery_allowed") == NULL) {
$type = htmlspecialchars($_POST['type']);}
$InsertQuery = new WA_MySQLi_Query($old_mule);
$InsertQuery->Action = "insert";
$InsertQuery->Table = "orders";
$InsertQuery->bindColumn("customerID", "i", "".((isset($customerID))?$customerID:"") ."", "WA_DEFAULT");
$InsertQuery->bindColumn("date_submitted", "s", "".((isset($today))?$today:"") ."", "WA_DEFAULT");
$InsertQuery->bindColumn("type", "s", "".((isset($type))?$type:"") ."", "WA_DEFAULT");
$InsertQuery->bindColumn("delivery_date", "t", "".((isset($delivery_date))?$delivery_date:"") ."", "WA_DEFAULT");
$InsertQuery->saveInSession("orderID");
$InsertQuery->execute();
$InsertGoTo = "";
if (function_exists("rel2abs")) $InsertGoTo = $InsertGoTo?rel2abs($InsertGoTo,dirname(__FILE__)):"";
$InsertQuery->redirect($InsertGoTo);
$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", "".((isset($orderID))?$orderID:"") ."", "WA_DEFAULT");
$UpdateQuery->bindColumn("status", "s", "".((isset($status))?$status:"") ."", "WA_DEFAULT");
$UpdateQuery->addFilter("cartID", "=", "i", "".($cartID2) ."");
$UpdateQuery->execute();
}
$UpdateGoTo = "";
if (function_exists("rel2abs")) $UpdateGoTo = $UpdateGoTo?rel2abs($UpdateGoTo,dirname(__FILE__)):"";
$UpdateQuery->redirect($UpdateGoTo);
}
?>

Sign in to reply to this post

Mags

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.

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...