[英]Update database record based on the select query result
嗨,我想更新价格列,如果日期列包含今天的日期。 以下是我的代码,但是即使有两条相同日期的记录,它也只会更新一条记录。
码
<?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=2
不是entity_id=3
。 任何帮助表示赞赏。
亲爱的您使用相同的对象“ $ result”进行选择和更新查询,请更新名称“ $ resultUpdate”以进行更新查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.