How do I create an Audit Trail using Triggers in PHPMyAdmin on a shared hosting platform? [closed]

StackOverflow https://stackoverflow.com/questions/23142037

  •  05-07-2023
  •  | 
  •  

質問

I found this really great way to create triggers.. https://www.simple-talk.com/author/pop-rivett/

but I think this is only using sql server.. I am looking for a way to audit my very small (3mg) database to show every table change, who changed what, when, and where and maybe if they put in a note, why. :)

Please let me know if you have the code to create a "trigger" using the phpmyadmin sql tab or trigger creator to insert the code?? I am not really a coder at all just learning php and mysql over the last few months. Any help is most appreciated. I run a small non profit and are looking to track tagged manatees, and turtles and sharks in our database and want to 'dummy' proof the data against dummys who may make bad changes to the data.

Please help it is a good cause!

Thanks, Tina

役に立ちましたか?

解決

Perhaps something like:

DELIMITER $$
CREATE TRIGGER `AuditUserTrigger`
       BEFORE UPDATE ON `Users`
  FOR EACH ROW BEGIN
    INSERT INTO `aUsers`
         SELECT `Users`.*
           FROM `Users`
           INNER JOIN `AuditTables`
                   ON `AuditTables`.`name` = 'Users'
                  AND `AuditTables`.`enabled` = 1
          WHERE `Users`.`id` = OLD.`id`;
  END$$
DELIMITER ;

EDIT

By way of an explanation:

I have an Audit table defined as

CREATE TABLE IF NOT EXISTS `AuditTables` (
  `id` int(12) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(64) NOT NULL,
  `enabled` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `auditTable` (`name`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 
;

which contains an entry for every core table in the database, together with the active flag that can be set to true or false (1 or 0) depending on whether I want that table to be audited or not.

For my Users table (as shown in the original example) I have a corresponding audit table (aUsers) with an identical structure, but without unique keys, that can hold all the "historic" records from the Users table. The ON UPDATE trigger on Users will save the OLD Users table record to aUsers if auditing is enabled for the Users table before executing the actual UPDATE against the user record.

All records in all tables also have the following columns:

`archived` tinyint(1) NOT NULL DEFAULT '0',
`createdAt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`createdBy` int(12) unsigned NOT NULL DEFAULT 0,
`updatedAt` datetime ON UPDATE CURRENT_TIMESTAMP,
`updatedBy` int(12) unsigned DEFAULT NULL,

giving me details of who an when updates were done, while the archived flag is set as a "soft delete"

createdBy and updatedBy are managed by my code, with an element in my model layer that automatically injects the session user ID into that property.

updatedAt datetime ON UPDATE CURRENT_TIMESTAMP, requires MySQL 5.6

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top