duan00529 2014-05-22 14:54 采纳率: 100%
浏览 1020

触发器阻止INSERT

I have a html form that send some data in a database. I just created a trigger that on every people that complete the city field with LA will add the word "HI FROM LA" in a column named tag.

The problem is that after I created this trigger I get an error when I try to submit a form:

#1442 - Can't update table 'users' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. 

The trigger is:

DELIMITER |
CREATE TRIGGER update_tag BEFORE INSERT ON users
        FOR EACH ROW
        BEGIN
            IF (NEW.city= 'LA') THEN
            INSERT INTO users
            SET NEW.tag = 'HI FROM LA';
            END IF;
        END;
|
DELIMITER ;

The insert is done with:

INSERT INTO users(id, name, city) VALUES(23, "John", "LA")

What alternatives do I have to avoid this conflict?

EDITED TRIGGER:

I also created a new table called 'trigger':

DELIMITER |
CREATE TRIGGER update_tag BEFORE INSERT ON users
        FOR EACH ROW
        BEGIN
            IF (NEW.city= 'LA') THEN
            INSERT INTO trigger
            SET NEW.tag = 'HI FROM LA';
            END IF;
        END;
|
DELIMITER ;

With this trigger, I get an error on line 5

  • 写回答

3条回答 默认 最新

  • donglu1881 2014-05-22 15:04
    关注

    You don't need to work with triggers for this.

    The trigger you have showed us would even create an endless loop, if it were to work.

    Just use your insert query like this:
    insert into users set (city, tag) values ('LA', concat('HI FROM ', city));

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料