I am trying to insert multiple rows at once to my db, I posted a question here the other day see here. Regarding my insert query only insert one row into the db, even if it was necessary for a 2 or more rows of data to be inserted into the DB. Due to the fact this insert query is inserting shopping cart data i am unaware of the number of items a user may insert into the db.
After spending a few days researching i found the method i am now using may be the best way to resolve the issue, this is my first time using a foreach loop, and creating a insert query this way, I am receiving the follow error:
Fatal error: Uncaught Error: Call to a member function execute() on boolean
Which i am aware means my prepare statement failed, but i was hoping someone could explain or demonstrate where i have gone wrong, as i am trying to learn from my mistakes. I am not sure if it is the way i have created my query or even the way i have executed it.
Code
$_query = "INSERT INTO ord_dets(Order_ID,custmer_ip,Resturant_ID,Resturant_name,City_name,
Product_Id,Product_Name,Product_Price,item_sub)
VALUES ";
$_query_parts = array();
for($x=0;$x<count($OI); $x++){
$_query_parts = "('" . $OI[$x] . "', '" . $ip[$x] . "','" . $_SESSION['rest_id'][$x] . "','" . $rest_name[$x] . "',
'" . $City_name[$x] . "','" . $Product_Id[$x] . "','" . $product_name[$x] . "','" . $prod_price[$x] . "','" . $item_sub[$x] . "')";
}
$query_run = $dbc->prepare($_query);
echo $_query;
if (!$query_run->execute()) {
$insertError = "There was an error inserting data: " . $query_run->error;
print "affected rows:" . $query_run->affected_rows; //how many records affected?
}
}