我在購買表格和更新設施表格中插入問題。比方說,用戶通過product_id和product_quantity進行了購買。在當前表格中插入數據並更新另一個表格MySQL
查詢正在運行。但它插入兩次相同的數據,而不是更新設施表。
當用戶點擊提交時,我想將product_id和product_quantity插入到購買表中。並使用與其關聯的product_id和product_quantity更新設施表。
這裏是我的代碼
<?php
include 'dbconn.inc.php';
include 'functions.inc.php';
$sql1 = "SELECT * FROM facilities";
$res = $mysqli->query($sql1);
$facilities = array();
while($row = $res->fetch_array(MYSQLI_ASSOC)){
$facilities[]['id'] = $facilities_id;
$facilities[]['product_id'] = $facilities_product_id;
$facilities[]['product_current_quantity'] = $product_current_quantity;
}
$id = $mysqli->real_escape_string ($_POST['id']);
$purchase_id = $mysqli->real_escape_string($_POST['purchase_id']);
$facility_id = $mysqli->real_escape_string($_POST['facility_id']);
$product_quantity = $mysqli->real_escape_string($_POST['product_quantity']);
$sql1 = "UPDATE facilities
SET
`product_current_quantity` = '$product_quantity + $product_current_quantity'
WHERE $facility_id = $facilities_id AND $id = $facilities_product_id ";
$sql = "INSERT INTO purchases
(
`purchase_id`,
`facility_id`,
`product_quantity`,,
`product_id`
)
VALUES
(
'$purchase_id',
'$facility_id',
'$product_quantity',
'$id'
)";
我做了一些研究,我想我需要使用觸發器。但我從來沒有使用過觸發器。任何幫助都會很棒。謝謝!
「*同時*」在數據庫中沒有任何意義。總是有訂單。你面臨的實際問題是什麼? – Shoe 2013-05-04 04:06:35
SQL看起來不錯,但你沒有在代碼中運行它。如果你正在運行,並且數據庫沒有正確更新/更新,請說出來。 – 2013-05-04 04:16:14
查詢正在運行,但它在購買表中插入了兩次數據,根本沒有更新設施表。 – user1754822 2013-05-04 04:22:08