嗨我想更新價格列,如果日期列包含今天的日期。以下是我的代碼,但它只更新一個記錄,即使有兩個相同日期的記錄。根據選擇查詢結果更新數據庫記錄
代碼
<?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
。任何幫助讚賞。
@linktoahref OP在while循環中檢查當前是否是「splprice_from」,然後使用entity_id(在while循環中來自同一記錄)更新記錄。 –
@TomUdding有兩行有相同的日期。所以我想更新兩個記錄的價格。 –
其中一個原因可能是時區不匹配。 – Jigar