我有一个简单的INSERT查询,在按钮的事件点击时运行,这会获取变量并将它们插入到order
表中。
我还想更新我的bands
表,将库存减少1,直至达到0,然后显示空或售罄,而不是进入负数,例如:-6
知道我可以使用哪个频段:
WHERE Band_id = $Band_id");
在同一声明中。
当前的INSERT看起来像:
"INSERT INTO orders (band_id,user_id,user_name,band_name,band_venue) VALUES('$Band_id', '$user_id', '$user_name', '$name', '$venue')";
结论
需要将我的INSERT更改为UPDATE WHILE,检查它是当前选定的波段。
答案提供检查
这个解决方案已经提供了很好的解决方案,但是我还不够熟练,无法理解为什么它的功能不正常。
$sql = "INSERT INTO orders (band_id,user_id,user_name,band_name,band_venue) VALUES('$Band_id', '$user_id', '$user_name', '$name', '$venue')";
$sql_create ="CREATE TRIGGER tg_ai_orders
AFTER INSERT ON orders
FOR EACH ROW
UPDATE bands
SET stock = stock - 1
WHERE band_id = NEW.band_id";
$sql_deleat = "CREATE TRIGGER tg_ad_orders
AFTER DELETE ON orders
FOR EACH ROW
UPDATE bands
SET stock = stock + 1
WHERE band_id = OLD.band_id";
mysql_query ($sql, $sql_deleat, $sql_create, $linkme)
or die ("could not add to database");
答案 0 :(得分:1)
如果您使用触发器方法,那么您将需要两个触发器(用于插入和删除)
CREATE TRIGGER tg_ai_orders
AFTER INSERT ON orders
FOR EACH ROW
UPDATE bands
SET stock = stock - 1
WHERE band_id = NEW.band_id;
CREATE TRIGGER tg_ad_orders
AFTER DELETE ON orders
FOR EACH ROW
UPDATE bands
SET stock = stock + 1
WHERE band_id = OLD.band_id;
这是 SQLFiddle 演示