yesman
yesman

Reputation: 7829

PhpMyAdmin not handling trigger

I currently have:

delimiter //

CREATE TRIGGER blog_creator AFTER INSERT ON news

IF (EXISTS(SELECT * FROM news WHERE headline = 'LastItem' 
THEN 
INSERT INTO `blog` (`id`, `title`, `content`) VALUES (500, 'Hi', 'An item was posted!');

delimiter ;

Phpmyadmin isn't picking this up. No error message, just a loading symbol then nothing. SHOW TRIGGERS shows nothing, and inserting into news also doesn't do anything. What am I doing wrong?

Upvotes: 0

Views: 148

Answers (1)

Zagor23
Zagor23

Reputation: 1963

You are missing few things. Try this:

delimiter //

CREATE TRIGGER blog_creator AFTER INSERT ON news
FOR EACH ROW --always add this line
BEGIN -- start code block
    -- I think it would be faster to do the count instead of exists
    IF ((SELECT COUNT(*) FROM news WHERE headline = 'LastItem' ) > 0) 
    THEN 
    INSERT INTO `blog` (`id`, `title`, `content`) VALUES (500, 'Hi', 'An item was posted!');
    END IF; --you need to end if
END// --you need to end trigger using the new delimiter

delimiter ;

You can try out SQLFiddle here:

http://sqlfiddle.com/#!2/524b4/1

Upvotes: 2

Related Questions