我读过其他一些像我这样的帖子,但没有一个能解决我的问题, 我有两个表,用户和购买,当用户注册时,我也想在购买表中添加相同的user_id。我尝试用这样的PHP代码:
$query = "CREATE TRIGGER `purchase_insert` AFTER INSERT ON `user` FOR EACH ROW BEGIN INSERT INTO purchase (user_id) VALUES (NEW.user_id)";
$result = mysqli_query($connection, $query);
if($result){echo "<br>TRIGGER Success!";} else {die("<br>Database query failed. " . mysqli_error($connection));}
这是我得到的错误:
Database query failed. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
如果问题重复,我很抱歉。
答案 0 :(得分:0)
它应该回来打勾而不是单引号
CREATE TRIGGER `purchase_insert` AFTER INSERT ON `user` FOR EACH ROW BEGIN INSERT INTO purchase (user_id) VALUES (NEW.user_id)
<强>更新强>
试试这个
DELIMITER$$
CREATE TRIGGER `purchase_insert`
AFTER INSERT ON `user`
FOR EACH ROW
BEGIN
INSERT INTO purchase (user_id) VALUES (NEW.user_id);
END$$
DELIMITER ;