0

I've tried to create trigger for each new post here is my code

CREATE TRIGGER insertprod AFTER INSERT ON post
FOR EACH ROW
BEGIN
  INSERT INTO attributes SET attrtext = 'sometext', post_id = NEW.id;
END;

I'm getting this error

enter image description here

3 Answers 3

2

You probably just need a delimiter:

DELIMITER $$
CREATE TRIGGER insertprod AFTER INSERT ON post
FOR EACH ROW
BEGIN
  INSERT INTO attributes SET attrtext = 'sometext', post_id = NEW.id;
END;
$$
DELIMITER ;

However, you are also using a non-standard form of INSERT. I would suggest:

DELIMITER $$
CREATE TRIGGER insertprod AFTER INSERT ON post
FOR EACH ROW
BEGIN
  INSERT INTO attributes(attrtext, post_id)
     VALUES('sometext', NEW.id);
END;
$$
DELIMITER ;
Sign up to request clarification or add additional context in comments.

Comments

0

The parser is confused by the ; that terminates the statement inside the trigger's BEGIN...END block. You need to use a DELIMITER statement to define a new delimiter, which you can then use to end the CREATE TRIGGER statement.

Comments

0

Your INSERT command is wrong. It should have been

 INSERT INTO attributes (attrtext, post_id) VALUES ('sometext', NEW.id);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.