gpt4 book ai didi

php - 如果表 1 中存在值,则插入表 2 mysql

转载 作者:太空宇宙 更新时间:2023-11-03 12:14:02 25 4
gpt4 key购买 nike

对于我的订购系统,当管理员插入订单时,我需要检查他为订单插入的客户是否存在于我的客户表中。
我正在尝试这样的事情,但没有运气..

$sql = "IF EXISTS(SELECT * FROM customer WHERE customer_id = '$customer')
THEN insert into `order` (customer_id, product, quantity, creation_time, order_note, order_employee)
values ('$customer', '$product', '$quantity', 'now()', '$note', '$employee')";

错误:第一行有语法错误。这里有什么问题?
这是正确的方法吗?有没有更好的办法?

最佳答案

你可能想“向后”做 - 如果存在则插入:

$sql = "insert into `order` (customer_id, product, quantity, creation_time, order_note, order_employee)
select '$customer', '$product', '$quantity', now(), '$note', '$employee'
from dual
where EXISTS(SELECT * FROM customer WHERE customer_id = '$customer')";

关于php - 如果表 1 中存在值,则插入表 2 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23013399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com