php - Create a mysql trigger to insert data when a column updated -
create trigger `update_2` after update on `itm_master` each row begin if new.transfer_status='yes' insert activity_tbl (`evnt_date`,`con_type`,`username`,`item_serial`,`item_model`,`item_type`,`to_status`) values (now(),'update',new.user,new.item_serial,new.master_item_model,new.master_item_type,new.item_status); end if; end
i'd create trigger insert data activity_tbl whenever transfer_status field updated in item_master table. use query receiving mysql error
create trigger `update_2` after update on `itm_master` each row begin if new.transfer_status='yes' insert activity_tbl (`evnt_date`,`con_type`,`username`,`item_serial`,`item_model`,`item_type`,`to_status`) values (now(),'update',new.user,new.item_serial,new.master_item_model,new.master_item_type,new.item_status);
mysql said: documentation
1064 - erreur de syntaxe près de '' à la ligne 7
i got work. out of creative commentary @ moment.
schema:
create table itm_master ( id int auto_increment primary key, transfer_status varchar(100) not null, user varchar(100) not null, item_serial varchar(100) not null, master_item_model varchar(100) not null, master_item_type varchar(100) not null, item_status varchar(100) not null ); create table activity_tbl ( id int auto_increment primary key, `evnt_date` datetime not null, `con_type` varchar(100) not null, `username` varchar(100) not null, `item_serial` varchar(100) not null, `item_model` varchar(100) not null, `item_type` varchar(100) not null, `to_status` varchar(100) not null );
trigger:
drop trigger if exists update_2; delimiter $$ create trigger `update_2` after update on `itm_master` each row begin if new.transfer_status='yes' insert activity_tbl (`evnt_date`,`con_type`,`username`,`item_serial`,`item_model`,`item_type`,`to_status`) values (now(),'update',new.user,new.item_serial,new.master_item_model,new.master_item_type,new.item_status); end if; end;$$ delimiter ;
test:
insert itm_master(transfer_status, user, item_serial, master_item_model, master_item_type, item_status) values ('a','b','c','d','e','f'); update itm_master set transfer_status='blah' id=1; select * activity_tbl; -- no rows update itm_master set transfer_status='yes' id=1; select * activity_tbl; +----+---------------------+----------+----------+-------------+------------+-----------+-----------+ | id | evnt_date | con_type | username | item_serial | item_model | item_type | to_status | +----+---------------------+----------+----------+-------------+------------+-----------+-----------+ | 1 | 2016-09-13 00:14:26 | update | b | c | d | e | f | +----+---------------------+----------+----------+-------------+------------+-----------+-----------+
the delimiter
wrapper not needed phpmyadmin.
Comments
Post a Comment