1

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.

4

4 Answers 4

1

What I can see from your code It is not the issue or error of PHP or Mysql. Its issue of your logic i.e.:

foreach ($prices as $key => $value) {
         $xx = $value;

         $query2 = $this->dbh->prepare("UPDATE prices SET discount_price = ?");
         $query2->execute(array( $xx)); 
    }

The update statment execute each and every time and replace all price of table. So you are getting last updated value which replace all previous update statements

Sign up to request clarification or add additional context in comments.

4 Comments

if i write the insert code outside the loop then $xx value is becomes null
what exactly you want. show us an example with database
Thanks bro.. you are right issue is in my logic. Finally Solved it.
There is no reason to declare the prepare() inside the loop -- it never changes, but is designed to be used over and over.
0

$xxis created before any iteration and set to null. During each iteration if will be overwritten. It will not be destroyed at any time before leaving the scope of your script, function, method, ...

$xx;
foreach ($prices as $key => $value) {
         $xx = $value;
         $query2 = $this->dbh->prepare("UPDATE prices SET discount_price = ?");
         $query2->execute(array( $xx)); 
    }

1 Comment

There is no reason to declare the prepare() inside the loop -- it never changes, but is designed to be used over and over.
0

For php variable declare inside loop you have to check this Are PHP variables declared inside a foreach loop destroyed and re-created at each iteration? for your issue.

Also another thing you should check your array is not empty before loop :

And you have to add condition for your update query check some good stuff for mysql :

$xx; 
if(!empty($prices)){
foreach ($prices as $key => $value) {
         $xx = $value;
         $query2 = $this->dbh->prepare("UPDATE prices SET discount_price = ?");
         $query2->execute(array( $xx)); 
    }
}

2 Comments

declare it but still the last element is getting saved
There is no reason to declare the prepare() inside the loop -- it never changes, but is designed to be used over and over.
0

I Guess You Can do is That use $key

$key is a Indexing of Array Values in $prices Array

foreach ($prices as $key => $value) {
         $query2 = $this->dbh->prepare("UPDATE prices SET discount_price = ?");
         $query2->execute(array($value[$key])); 
    }

Hope This Helps.

1 Comment

There is no reason to declare the prepare() inside the loop -- it never changes, but is designed to be used over and over.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.