0
I have two tables: the requested table and the logs table. Users can interact with the requested table, link a php script that runs a query update and update the status column. I need to create a Trigger that, when running this update query, saves the change in the log table.
I used this Rigger, but now does not update either the requested table or the log table:
DELIMITER $$
CREATE TRIGGER salva_status
BEFORE UPDATE ON pedidos
FOR EACH ROW BEGIN
INSERT INTO logs
SET acao = 'update',
nPedido = OLD.nPedido,
status = OLD.status,
modificadoem = NOW(); END$$
DELIMITER ;
These are the query for table creation:
This table is the one that stores the order information
CREATE TABLE `localhost`.`pedidos` ( `id` INT NOT NULL AUTO_INCREMENT , `emissaoPed` INT NOT NULL ,
`nPed` INT NOT NULL ,
`NrPedido` VARCHAR(20) NOT NULL ,
`nomeAbrev` VARCHAR(50) NOT NULL ,
`vlr` FLOAT NOT NULL ,
`status` VARCHAR(20) NOT NULL ,
UNIQUE (`id`)) ENGINE = InnoDB;
And this is the table that will store the logs:
CREATE TABLE `root`.`logs` (
`id` INT NOT NULL AUTO_INCREMENT ,
`NrPedido` VARCHAR(20) NOT NULL ,
`antigoStatus` VARCHAR(20) NOT NULL ,
`novoStatus` VARCHAR(20) NOT NULL ,
`modificadoem` DATE NOT NULL ,
`vlr` FLOAT NOT NULL ,
`status` VARCHAR(20) NOT NULL ,
UNIQUE (`id`)) ENGINE = InnoDB;
Try to put
AFTER UPDATE ON pedidos
see if anything changes– R.Santos
I changed the query and no effect :/
– Rick Parapinski
But let me ask you, when you want to insert a record into a table through a Trigger, you don’t need to use the
insert
in accordance with that link https://www.techonthenet.com/mysql/triggers/after_update.php ???– R.Santos
Dude Try to change this Insert.... INSERT INTO logs(acao,nPedido,Status) VALUES ('update',OLD.nPedido, OLD.status, NOW());
– Wpbarcelos
sends the creates Tables I do to Trigger
– Wpbarcelos
Not
INSERT INTO logs SET...
isINSERT INTO logs VALUES
orINSERT INTO logs (acao, nPedido, status, modificagem) VALUES (...)
– adventistaam
So, guys, I put the query I used to create the tables to make it clearer
– Rick Parapinski