Bilal Saqib Bilal Saqib - 2 months ago 9
MySQL Question

Why this Trigger is not executing?

I am using a

trigger
to fire after the insert in the admitted_student
using mysql.
This is my
trigger` code:

DELIMITER //

CREATE TRIGGER Record_after_insert
AFTER INSERT
ON admitted_student FOR EACH ROW

BEGIN

DECLARE who varchar(50);
DECLARE IDD int (15);
SELECT admitted_student_id FROM admitted_student ORDER BY admitted_student_id DESC into IDD;
SELECT USER() INTO who;
INSERT INTO student_audit
( id,
attime,
who)
VALUES
( IDD,
SYSDATE(),
who);
END;
DELIMITER ;


This is my table on which I am using trigger:

enter image description here

And this the table for my Trigger:

enter image description here

When I Execute the trigger code then it gives me the following Error:

enter image description here

Answer

For non-PHPMyAdmin try:

DROP TRIGGER IF EXISTS Record_after_insert;
DELIMITER //
CREATE TRIGGER Record_after_insert
AFTER INSERT
   ON admitted_student FOR EACH ROW

BEGIN
   INSERT INTO student_audit (action,id,attime,who)
   SELECT 'something',NEW.admitted_student_id,now(),CURRENT_USER();
END; //
DELIMITER ;

for PHPMyAdmin try:

DROP TRIGGER IF EXISTS Record_after_insert;
CREATE TRIGGER Record_after_insert
AFTER INSERT
   ON admitted_student FOR EACH ROW

BEGIN
   INSERT INTO student_audit (action,id,attime,who)
   SELECT 'something',NEW.admitted_student_id,now(),CURRENT_USER();
END;

PHPMyAdmin doesn't user a DELIMITER. CURRENT_USER() is the user you resolved to on login.

who VARCHAR(10) ought to be like VARCHAR(100).

attime should be a DATETIME not what it is.