2012-11-12 30 views
0

這就是我想要做的。 如果一個人買一盞燈,product_quantity將上浮了1 另一個人購買同樣的產品,但「product_quantity」是3 所以,如果總額高達其4使用sql添加一個數字

這是我的代碼:

// build SQL statement to select how many quantities of that product. 
$query = "select product_quantity from table where order_item_name = '$ordername' and order_id='$orderid'"; 

// execute SQL statement 
$result = mysqli_query($link, $query) or die(mysqli_error($link)); 

// shows how many quantity of that product 
while($row= mysqli-fetch_assoc($result)){ 
    $lol = $row['product_quantity'] 
} 

但是,如何使用SQL將此數字添加到表中的現有數字? 例如,在該表中,有一個名爲total_order場:10 我希望將新訂單增加:數量1和3到現有total_order:10同樣的產品,使其14

+0

您必須添加更新查詢以更新數據庫中的數量。 – admoghal

+0

但如果我更新1 total_order,將total_order更新爲'1'而不是'11'? –

+0

嘗試''更新表設置product_quantity = product_quantity + 1其中order_item_name ='$ ordername'和order_id ='$ orderid'「' – GBD

回答

0

使用任何上的數據庫列數學運算可以通過使用列中,操作的名稱來實現,和可變(例如:UPDATE TABLE SET COLUMN = COLUMN(+ - * /)$變量WHERE 1)

$ordername = 'lamp'; 
$orderid = 1; 

$total_quantity = 0; 
while($row= mysqli->fetch_assoc($result)){ 
    $total_quantity += $row['product_quantity']; 
}  
$sql = "UPDATE TABLE SET `product_quantity`= product_quantity + $total_quantity WHERE `order_item_name` = '$ordername' AND `order_id`='$orderid'"; 
+0

但如果有一個像我上面的示例一樣的while循環來獲取每個訂單的數量產品。我如何添加它們? //訂購燈總數量 $ total_quantity = $ lol + $ lol; ? –

+0

我已經更新了編碼部分,以便更適合您的情況。 –