Error saving database log

Asked

Viewed 155 times

0

I have a database with Mysql. I am doing the log of this bank. Most are working, but I’m not able to get the id of the record that was modified.

Example: when changing some data, he should be saved in log the id of the registry that was changed, but only appears NULL. I’m making them use triggers, and one of them is:

CREATE TRIGGER `empresa_AFTER_INS` AFTER INSERT ON `tabela` FOR EACH ROW
insert into `log`.`log_tabela`(campos_do_banco,data_hora,acao,usuario)
values (NEW.campos_do_banco,NOW(),'insert',@usuario);

Like I do so that the id of the data be saved in the records of log?

  • 1

    Wouldn’t that be what you want? values (OLD.Id, NOW(), 'insert', @usuario);?

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

1 answer

1

It seems to me that just the filling is wrong, it would be something like this (depends on what you want to record and how you will organize the log):

CREATE TRIGGER `empresa_AFTER_INS` AFTER INSERT ON `tabela` FOR EACH ROW
    insert into `log`.`log_tabela`(campos_do_banco, data_hora, acao, usuario)
        values (NEW.Id, NOW(), 'insert', @usuario);

I put in the Github for future reference.

  • i later saw that I had forgotten to send id ;) Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.