在下面的代码中,我试图创建一个插入' incidentimg'如果网络应用程序的用户没有上传具有属于' incidentreport'的表单的图像,则表ID和图像路径但是,我只得到错误" MySQL说:#1422 - 存储函数或触发器中不允许显式或隐式提交"
DROP TRIGGER IF EXISTS `insertImage`;
CREATE TRIGGER `insertImage` AFTER INSERT ON `incidentreport`
FOR EACH ROW
BEGIN
IF(SELECT incidentimg.incidentImgId FROM incidentimg
LEFT JOIN incidentreport ON incidentimg.imgs_incidentReportId = incidentreport.incidentReportId
WHERE incidentimg.incidentImgId IS NULL)
THEN INSERT INTO incidentimg(incidentImgId, imgUrl)values(NEW.incidentReportId, 'images/no-image.png');
END IF;
END
;
以下是正在使用的表格
有人可以帮我解决这个问题吗
答案 0 :(得分:1)
我终于让代码工作了。现在代码将检查'incidentimg'表是否具有'incidentreport'表所具有的所有'incidentReportId',如果没有,则将插入'incidentreport'表中最后插入的id以及默认图像路径进入'incidentimg'表后插入。
CREATE TRIGGER insertImage AFTER INSERT ON incidentreport
FOR EACH ROW
BEGIN
IF(SELECT incidentreport.incidentReportId
FROM incidentreport
LEFT JOIN incidentimg ON incidentreport.incidentReportId = incidentimg.imgs_incidentReportId
WHERE incidentimg.imgs_incidentReportId IS NULL)
THEN INSERT INTO incidentimg(imgs_incidentReportId, imgUrl) VALUES(NEW.incidentReportId, 'images/no-image.png');
END IF;
END
答案 1 :(得分:0)
我不太确定,但我猜这可能是因为IF .. THEN
条件INSERT
。尝试使用INSERT
之类的<{p>}修改EXISTS
语句
DROP TRIGGER IF EXISTS `insertImage`;
CREATE TRIGGER `insertImage` AFTER INSERT ON `incidentreport`
FOR EACH ROW
BEGIN
INSERT INTO incidentimg(incidentImgId, imgUrl)
SELECT NEW.incidentReportId, 'images/no-image.png'
FROM DUAL WHERE EXISTS (
SELECT 1 FROM incidentimg
LEFT JOIN incidentreport
ON incidentimg.imgs_incidentReportId = incidentreport.incidentReportId
WHERE incidentimg.incidentImgId IS NULL
)
END;