mysql - The right syntax for handling an UPDATE trigger and logging what happened per column? -


the users table:

create table `users` (   `id` int(8) unsigned not null auto_increment,   `email` varchar(45) default null,   `username` varchar(16) default null,   `salt` varchar(16) default null,   `password` varchar(128) default null,   `lastlogin` timestamp not null default '0000-00-00 00:00:00',   `joined` timestamp not null default current_timestamp,   `loggedin` tinyint(1) unsigned not null default '0',   `sessionkey` varchar(60) default null,   `verifycode` varchar(16) default null,   `verified` tinyint(1) unsigned not null default '0',   `banned` tinyint(1) unsigned not null default '0',   `locked` tinyint(1) unsigned not null default '0',   `ip_address` varchar(45) default null,   `failedattempts` tinyint(1) unsigned not null default '0',   `unlocktime` timestamp not null default '0000-00-00 00:00:00',   primary key (`id`) ) engine=innodb auto_increment=8 default charset=latin1; 

the user_records table:

create table `user_records` (   `id` int(8) unsigned not null auto_increment,   `userid` int(8) unsigned default null,   `action` varchar(100) default null,   `oldentry` varchar(100) default null,   `newentry` varchar(100) default null,   `timestamp` timestamp not null default current_timestamp,   primary key (`id`) ) engine=innodb auto_increment=6 default charset=latin1; 

my trigger code users table insert on user_records table:

    use `gknet`;    delimiter $$    create trigger `after_update_user` after update on `users` each row begin     if new.loggedin != old.loggedin or new.sessionkey != old.sessionkey         if new.loggedin = '1'             insert users (new.lastlogin)             values (now());             insert user_records (userid, action, oldentry, newentry, timestamp)             values (new.id,'logged_in',old.sessionkey,new.sessionkey,now());         else if new.loggedin = '0'             insert user_records (userid, action, oldentry, newentry, timestamp)             values (new.id,'logged_out',old.sessionkey,new.sessionkey,now());         end if;     end if;      if new.locked != old.locked or new.unlocktime != old.unlocktime         if new.locked = '1'             insert user_records (userid, action, oldentry, newentry, timestamp)             values (new.id,'locked',old.unlocktime,new.unlocktime,now());         else if new.locked = '0'             insert user_records (userid, action, oldentry, newentry, timestamp)             values (new.id,'unlocked',old.unlocktime,new.unlocktime,now());         end if;     end if;      if new.ip_address != old.ip_address         insert user_records (userid, action, oldentry, newentry, timestamp)         values (new.id,'ip_change',old.ip_address,new.ip_address,now());     end if;      if new.verified != old.verified         if new.verified = '1'             insert user_records (userid, action, timestamp)             values (new.id,'verified',now());         else if new.verified = '0'             insert user_records (userid, action, timestamp)             values (new.id,'unverified',now());         end if;     end if; end $$ 

mysql keeps saying there error near line 38 (second end if bottom). if remove whole if statement, it'll give error again 1 above it. how fix this? correct syntax?

edit: replacing else if elseif doesn't help:

use `gknet`; delimiter $$ create trigger `after_update_user` after update on `users` each row begin if new.loggedin != old.loggedin or new.sessionkey != old.sessionkey     if new.loggedin = '1'         insert users (new.lastlogin)         values (now());         insert user_records (userid, action, oldentry, newentry,  timestamp)         values (new.id,'logged_in',old.sessionkey,new.sessionkey,now());     elseif new.loggedin = '0'         insert user_records (userid, action, oldentry, newentry, timestamp)         values (new.id,'logged_out',old.sessionkey,new.sessionkey,now());     end if; end if;  if new.locked != old.locked or new.unlocktime != old.unlocktime     if new.locked = '1'         insert user_records (userid, action, oldentry, newentry, timestamp)         values (new.id,'locked',old.unlocktime,new.unlocktime,now());     elseif new.locked = '0'         insert user_records (userid, action, oldentry, newentry, timestamp)         values (new.id,'unlocked',old.unlocktime,new.unlocktime,now());     end if; end if;  if new.ip_address != old.ip_address     insert user_records (userid, action, oldentry, newentry, timestamp)     values (new.id,'ip_change',old.ip_address,new.ip_address,now()); end if;  if new.verified != old.verified     if new.verified = '1'         insert user_records (userid, action, timestamp)         values (new.id,'verified',now());     elseif new.verified = '0'         insert user_records (userid, action, timestamp)         values (new.id,'unverified',now());     end if; end if; 

end; $$

this simple typographical error.

mysql syntax uses elseif (with no spaces), rather else if in context of if ... end if block.

the else if valid, starts whole 'nother if block, needs ended end if.

http://dev.mysql.com/doc/refman/5.5/en/if.html


this work:

if foo   stmt; elseif bar    stmt; end if; 

or work too:

if foo   stmt; else   if bar      stmt;   end if; end if; 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -