根据选择查询结果更新数据库记录

时间:2017-04-22 05:21:33

标签: php mysql

如果日期列包含今天的日期,我想更新价格列。以下是我的代码,但它只更新一条记录,即使有两条相同日期的记录。

代码

<?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'];
             }
         }
    }

?>

打印结果

    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 ) 

以上更新查询会更新entity_id=2的价格,而不是entity_id=3。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:2)

亲爱的,您已为选择和更新查询使用相同的对象“$ result”,请更新名称“$ resultUpdate”以进行更新查询。