0
私の製品の数量列を更新するFOR LOOPの内部でクエリを作成しようとしています。顧客がチェックアウトすると、注文テーブルに行が作成されます。そこから、購入されたすべての商品の注文明細に行が作成されます。OOP ForループSQL更新クエリ数量
私は
- 製品(ID、数量)
- たOrderDetails(ID、受注コード、商品コード、数量) "受注コードと商品コードが外部キーです"
- 注文(次の列を持つ3つのテーブルを持っていますID)
私は受け付けており、エラーが
です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();
}
なぜこのエラーが発生しているのですか?
ご協力いただきありがとうございます。これは私のエラーメッセージを解決しましたが、私のクエリは指定された製品テーブルの行からすべての数量を削除します。悲しいことに、 – Oleeze
Hmm ..テーブル 'products'の' Quantity'列が 'INT'ではなく' VARCHAR'であることを再確認できますか? – Ivan86
多分それはタイプミスで、それは 'int'だったはずです。これを試してください: '$ update = $ conn-> prepare(" UPDATE products SET Quantity = "。$ cart [$ i] - > quantity。" WHERE ID = "。$ cart [$ i] - > id);' – Ivan86