0

FOUND SOLUTION !!!!

CREATE TRIGGER `triggerau_user` AFTER INSERT ON `customers`
 FOR EACH ROW INSERT INTO au_user (nome, datahora) values (new.name,new.created)

Found pretty strange that i doesnt use BEGIN nor `END, but it worked, is doing fine, every time i insert into the table customer it makes the trigger and inserts on the au_user


After about 1.5 year, i come back and need to work with MySQL again, so i am a bit rusty on the subject, i can do the basics right, but something about phpadmin and the mysql interface on it doesnt get to me, i cant do simple triggers, and i want to know what is the mistake in those.

So i have these 2 tables

CREATE TABLE au_user (nome varchar(100), datahora datetime);

CREATE TABLE customer (name varchar(100), created datetime);

the customer table, picks data from my crud/website, with no problem at all(the table is bigger), both of these are fine, but now for the question how do i pick name and created and save on au_user nome and datahora.

    CREATE TRIGGER triggerau_user AFTER INSERT ON customers
FOR EACH ROW
BEGIN
    INSERT INTO au_user (nome, datahora)
SELECT 
    name, created
FROM customers
END;

it always says that i have a error near line 8 END; but how can i fix this, and sorry for the incovenience.

  • change this after BEGIN and let me know whether it works: INSERT INTO au_user (nome, datahora) values (new.name,new.created) END; – Harshil Doshi Sep 19 '17 at 18:57
  • still says that i have an error in the END line, https://imgur.com/a/4QRwy – Nomad Brasil Sep 19 '17 at 19:01
  • Possible duplicate of [How to program a MySQL trigger to insert row into another table?](https://stackoverflow.com/questions/4753878/how-to-program-a-mysql-trigger-to-insert-row-into-another-table) – pucky124 Sep 19 '17 at 19:04
  • i didnt work for me, it didnt even compile if i copy and paste on the interface. – Nomad Brasil Sep 19 '17 at 19:07
  • in create query, table name is customer while in trigger you wrote customers. Is it just typo here in question? – Harshil Doshi Sep 19 '17 at 19:10
  • `CREATE TRIGGER triggerau_user AFTER INSERT ON customers FOR EACH ROW INSERT INTO au_user (nome, datahora) values (new.name,new.created)`, this works, with no begin nor end, just a typo @Harshil – Nomad Brasil Sep 19 '17 at 19:11

1 Answers1

0

This should work:

CREATE TRIGGER triggerau_user AFTER INSERT ON customer
FOR EACH ROW
BEGIN
   INSERT INTO au_user(nome, datahora) values(new.name,new.created); END;

I forgot to add ; after Insert query.

Hope it helps!

Harshil Doshi
  • 3,419
  • 3
  • 11
  • 30