I want to run update query using for loop and while loop. Because I want to update quantity of product in product table after success of order according to product id and total quantity after subtracting order quantities from total quantity.But the query does not run successfully.
My Code:
// Update Query
$update_quantity = "";
for ($i = 0; $i < (count($_SESSION['shopping_cart'])); $i++) {
$select_quan = "SELECT * FROM product WHERE pid = {$pid[$i]}";
$result_quan = mysqli_query($con, $select_quan);
// $update_quantity .= " UPDATE product SET ";
while ($row_quan = mysqli_fetch_assoc($result_quan)) {
$quantity_after_order[$i] = $row_quan['pquantity'] - ($pquantity[$i]);
$update_quantity .= "UPDATE product SET pquantity = '$quantity_after_order[$i]' WHERE pid=$pid[$i]; ";
}
}
When I Run Query:
// echo $update_quantity;
if ($con->query($update_quantity) == TRUE) {
echo "Data Successfully Updated";
}
else{
echo "Error: $update_quantity <br> $con->error";
}
Output It Gives:
Error: UPDATE product SET pquantity = '246' WHERE pid=7; UPDATE product SET pquantity = '11' WHERE pid=32;
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UPDATE product SET pquantity = '11' WHERE pid=32' at line 1
If Another Way to run this query which updates quantities in database according to product_id after order success so plz tell me.