Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8312021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:01:02+00:00 2026-06-08T20:01:02+00:00

I am having a problem with what I would assume is my logic behind

  • 0

I am having a problem with what I would assume is my logic behind how to deal with stored data relating to lines ordered within an e-commerce system.

I have two arrays. One array (orderedItems) keeps track of the various product id’s relating to ordered products. The other array (orderedItemQuantity) keeps track of various product id’s and also a quantity value of the product ordered.

So now I would like to begin inserting data into a table called order_items. This table has four columns, id(the orders id number), itemId, quantity and totalValue. So each item that has been ordered, there is a row added to this table with the information corresponding to it inserted.

Here is the code i had in mind:

// Loop through ordered items
if ($stmt = $link->prepare("
    SELECT name, price
    FROM items i
    IN (".join($_SESSION['orderedItems'], ',').")
"))
{   
    $stmt->execute();
    $stmt->bind_result($itemName, $itemPrice);
    while ($stmt->fetch())
    {
        $itemQuantity = sanitize($_SESSION['orderedItemQuantity']["$itemId"]);
        $lineTotal = number_format($itemPrice * $itemQuantity, 2);
        // Insert each item line to database \\
        $stmt2 = $link->prepare("
            INSERT INTO order_items SET
            id = ?,
            itemId = ?,
            itemQuantity = ?,
            lineValue = ?
        ");

        if (!$stmt2)
        {
            $error = "Error {$link->errno}  :  {$link->error}";
            include "$docRoot/html/main/error.html.php";
            exit();
        }
        if (!$stmt2->bind_param("iiis", $orderId, $itemId, $itemQuantity, $lineTotal))
        {
            $error = "Error {$link->errno}  :  {$link->error}";
            include "$docRoot/html/main/error.html.php";
            exit();
        }
        if (!$stmt2->execute())
        {
            $error = "Error {$link->errno}  :  {$link->error}";
            include "$docRoot/html/main/error.html.php";
            exit();
        }
        $stmt2->close();
    }
    $stmt->close();
}

However, when I try to run this I get the mysql error code 2014 : Commands out of sync; you can’t run this command now. This is triggered by the if (!stmt2) clause.

Could someone possibly tell me if I am way off here and this is simply just not the way that things work. And if this is the case, could you suggest a solution for what I am trying to do.

If on the other hand this is possible, could you please explain to me why it is not working for me. Because for love nor money… It just wont, and I assume that this idea is fundamentally flawed.

Thank you for your time in reading through this and any suggestions, input, advice or knowledge that you may be willing to share!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:01:04+00:00Added an answer on June 8, 2026 at 8:01 pm

    Sorry, but the error message you are seeing is the last of your problems here.

    I have two arrays

    This is plain wrong – it should be a single array.

    The other array (orderedItemQuantity) keeps track of various product id’s and also a quantity value of the product ordered

    erm, looking at the code this seems to be a nested array.

    if ($stmt = $link->prepare("
      SELECT name, price
      FROM items i
      IN (".join($_SESSION['orderedItems'], ',').")
    "))
    

    Why are you bothering with prepared statements when your splicing in literal values with no escaping?

    while ($stmt->fetch())
    …
    $stmt2 = $link->prepare(“

    You should never execute SQL statements inside loops.

    Working around the error message is trivial – just use a seperate database connection – but it’s not going to fix the deeper issues with your code – OTOH tidying up the rest of your code should also solve the error properly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem with a pesky linker error and would appreciate tips to
I'm having a problem. I would like to create Document object, and I would
I'm having a simple test design problem, which I would like to solve once
The problem I'm having is that my Bean Validation isn't working as I would
I'm having a problem with running a custom function within an Access database. Currently,
I'm having an annoyingly stubborn problem here, and I would appreciate it if anyone
I'm having problems working through how I would build times that increment by a
I am having problems finding a solution for what I thought would be straight
I have started having problems with my VPS in the way that it would
Basically i am having problems with my code - this is homework so would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.