Nitin Pawar
Nitin Pawar

Reputation: 263

Update database record based on the select query result

Hi i want to update the price column, if the date column contains today's date. Following is my code but it update one record only, even there are two records of same date.

Code

<?php   
    require('db.php');
    $tdate=date('Y-m-d');
    $query = 'SELECT * FROM marketplace_product WHERE splprice_from IS NOT NULL AND splprice_to IS NOT NULL';
    $result = mysqli_query($con, $query) or die(mysqli_error($con));
    if(mysqli_num_rows($result)>0)
    {
         while($row = mysqli_fetch_assoc($result))
         {

             if($row['splprice_from']==$tdate)
             {
                  print_r($row);
                 $ent=$row['entity_id'];
                 $updatePrice="UPDATE marketplace_product SET today_price=splprice WHERE entity_id=$ent";
                 $result = mysqli_query($con, $updatePrice) or die(mysqli_error($con));
                 if($result)
                     echo "Price updated for id".$row['entity_id'];
                 else
                     echo "Price not updated for id".$row['entity_id'];
             }
         }
    }

?>

Print result

    Array ( [entity_id] => 2 [mageproduct_id] => 531 [adminassign] => 0 [seller_id] => 14 [store_id] => 0 [status] => 2 [seller_product_code] => [seller_price] => 5000.00 [splprice] => 4000.00 [splprice_from] => 2017-04-22 [splprice_to] => 2017-04-28 [today_price] => 0.00 [seller_qty] => 0 [return_qty] => [stock_per_day] => [confirmed_qty] => [pending_qty] => [sold_qty] => [canceled_qty] => [sell_limit] => 4 [discount_price] => [seller_tax] => [vat] => [cst] => [gst] => [octroi] => [created_at] => 2017-02-27 17:12:17 [updated_at] => 2017-02-27 17:12:17 ) 
Array ( [entity_id] => 3 [mageproduct_id] => 532 [adminassign] => 0 [seller_id] => 13 [store_id] => 0 [status] => 2 [seller_product_code] => [seller_price] => 5000.00 [splprice] => 4000.00 [splprice_from] => 2017-04-22 [splprice_to] => 2017-04-28 [today_price] => 0.00 [seller_qty] => 0 [return_qty] => [stock_per_day] => [confirmed_qty] => [pending_qty] => [sold_qty] => [canceled_qty] => [sell_limit] => 6 [discount_price] => [seller_tax] => [vat] => [cst] => [gst] => [octroi] => [created_at] => 2017-02-28 09:47:38 [updated_at] => 2017-02-28 09:47:38 ) 

Above update query updates the price for entity_id=2 not for entity_id=3. Any help appreciated.

Upvotes: 0

Views: 509

Answers (1)

kaustubh
kaustubh

Reputation: 178

Dear you have use same object "$result" for select and update query please update the name "$resultUpdate" for update query.

Upvotes: 3

Related Questions