OOP For Loop SQL Update Query Quantity

Oleeze

I am trying to create a query inside of my FOR LOOP that will update my products Quantity column. When a customer checks out they create a row in my orders table, from there a row is created in orderdetails for every item purchased.

I have three tables with the following columns

  1. products (ID, Quantity)
  2. orderdetails (ID, OrderID, ProductID, Quantity) "OrderID and ProductID are foreign keys"
  3. orders (ID)

The error I am receiving is

Fatal error: Uncaught Error: Call to a member function execute() on boolean

$newOrder = $conn->prepare("INSERT INTO orders (UserID, Amount) Values ('{$_SESSION['u_id']}','{$_SESSION['$s']}')");
$newOrder->execute();
$ordersid = $newOrder->insert_id;   

//Save order details for new order
$cart = json_decode(json_encode($_SESSION['cart']));
//For loop with query that creates a new row for every item
for($i = 0; $i<count($cart); $i++){
    $new_Orderdetail = $conn->prepare("INSERT INTO `orderdetails` (`OrderID`,`ProductID`,`Price`,`Quantity`) VALUES(".$ordersid.",".$cart[$i]->id.",".$cart[$i]->price.",".$cart[$i]->quantity.")");
    $new_Orderdetail->execute();
    $new_Orderdetailid = $new_Orderdetail->insert_id;
    echo "$new_Orderdetailid </br>";
    $update = $conn->prepare("Update p SET Quantity = Quantity - ".$cart[$i]->quantity." FROM products p where ID = ".$cart[$i]->id"");
    $update->execute();
}

Can someone please help me solve why I am receiving this error?

Ivan86

As far as I see the field Quantity in table products is of type VARCHAR() so you must use '' to add the value.

EDIT: Since you now want the field to be of type INT, the '' are no longer required. I've update the code:

// Corrected code
$update = $conn->prepare("UPDATE products SET Quantity=".$cart[$i]->quantity." WHERE ID=".$cart[$i]->id);

And you had two "" without a dot at the end of the same statement, and by the way the "" aren't needed here if $cart[$i]->id is numeric:

// Sample from your code
FROM products p where ID = ".$cart[$i]->id"");

// Corrected to
FROM products p where ID=".$cart[$i]->id);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

SQL Query Update using a join

分類Dev

SQL Max (Quantity*ProductPrice)

分類Dev

Jquery cart quantity update button?

分類Dev

Update table inside a loop in a procedure pl/sql

分類Dev

How to execute batch SQL update query in Clojure?

分類Dev

Optimizing a Sql Server Query to update large list

分類Dev

SQL query in Crystal Reports doesn't update

分類Dev

SQL query – doing a loop but using an array of variables

分類Dev

Stripe immediate payment on each quantity update

分類Dev

How to update product.quantity in PrestaShop with PrestaSharp?

分類Dev

while / foreach loop updating quantity in mySQL

分類Dev

SQL: Reconcile quantity using same table

分類Dev

Sort the SQL table by name using maximum of quantity

分類Dev

if query is not update

分類Dev

Logical query processing phase of INSERT, DELETE, and UPDATE in SQL queries

分類Dev

How to detect is UPDATE sql query successed using Room?

分類Dev

SQL: sum of all columns in only one query using UPDATE

分類Dev

SQL Update query to replace last 4 characters in column

分類Dev

Single row UPDATE query in SQL takes more than 3 seconds

分類Dev

How do I break out of an sql query infinite loop

分類Dev

LOOPとQUERYを含む複雑なSQL

分類Dev

Auto update cart totals on cart item quantity change in Woocommerce

分類Dev

How to update cart quantity if item already exist in Cart - reactjs?

分類Dev

How to update a list of multiple records same as normal SQL Update query using entity framework core?

分類Dev

Tkinter - update ListBox in a loop

分類Dev

Optimize update request in loop

分類Dev

Meteor update in loop

分類Dev

Javascript for loop firebase update

分類Dev

PHP OOP How to process data returned by DB query

Related 関連記事

  1. 1

    SQL Query Update using a join

  2. 2

    SQL Max (Quantity*ProductPrice)

  3. 3

    Jquery cart quantity update button?

  4. 4

    Update table inside a loop in a procedure pl/sql

  5. 5

    How to execute batch SQL update query in Clojure?

  6. 6

    Optimizing a Sql Server Query to update large list

  7. 7

    SQL query in Crystal Reports doesn't update

  8. 8

    SQL query – doing a loop but using an array of variables

  9. 9

    Stripe immediate payment on each quantity update

  10. 10

    How to update product.quantity in PrestaShop with PrestaSharp?

  11. 11

    while / foreach loop updating quantity in mySQL

  12. 12

    SQL: Reconcile quantity using same table

  13. 13

    Sort the SQL table by name using maximum of quantity

  14. 14

    if query is not update

  15. 15

    Logical query processing phase of INSERT, DELETE, and UPDATE in SQL queries

  16. 16

    How to detect is UPDATE sql query successed using Room?

  17. 17

    SQL: sum of all columns in only one query using UPDATE

  18. 18

    SQL Update query to replace last 4 characters in column

  19. 19

    Single row UPDATE query in SQL takes more than 3 seconds

  20. 20

    How do I break out of an sql query infinite loop

  21. 21

    LOOPとQUERYを含む複雑なSQL

  22. 22

    Auto update cart totals on cart item quantity change in Woocommerce

  23. 23

    How to update cart quantity if item already exist in Cart - reactjs?

  24. 24

    How to update a list of multiple records same as normal SQL Update query using entity framework core?

  25. 25

    Tkinter - update ListBox in a loop

  26. 26

    Optimize update request in loop

  27. 27

    Meteor update in loop

  28. 28

    Javascript for loop firebase update

  29. 29

    PHP OOP How to process data returned by DB query

ホットタグ

アーカイブ