I'm trying to save data from an array by using foreach but it's only saving the last element of the array every time.
Here is my code:
public function demo($cakeId, $percent) {
$query = $this->dbh->prepare("SELECT price, price * $percent / 100 as dprice FROM prices WHERE cake_id = ? ");
$query->execute(array($cakeId));
$prices = $query->fetchAll(\PDO::FETCH_ASSOC);
foreach ($prices as $key => $value) {
$d_amount = $value['dprice'];
$price = $value['price'];
$final_price = $price - $d_amount;
}
$query2 = $this->dbh->prepare("UPDATE prices SET discount_price = ? WHERE cake_id = ?");
$query2->execute(array($final_price,$cakeId));
$return['data'] = [];
$return['message'] = "Discount Added";
$return['msgType'] = true;
return $return;
}
$prices is an array containing all the prices but when I use $prices in a loop it only returns the last element.
Please help me on solving this.
wherecondition in your update query???