Re: Saving each updated field in the history table with user id
As mentioned, I would suggest using triggers. You could setup a trigger as follows:
Please note that you will have to change the table/fields to match your schema
Delimiter ;
Delimiter $$
DROP TRIGGER IF EXISTS insert_employee_dates $$
CREATE TRIGGER insert_employee_dates BEFORE INSERT ON employees
FOR EACH ROW
SET NEW.date_401k_notify = DATE_FORMAT(NEW.date_401k_eligible ,'%Y-%m-%d'),
NEW.date_401k_notify = DATE_SUB(NEW.date_401k_notify,INTERVAL 1 MONTH),
NEW.date_updated = CURDATE()
$$
Delimiter ;
--
Delimiter ;
Delimiter $$
DROP TRIGGER IF EXISTS update_employee_dates $$
CREATE TRIGGER update_employee_dates BEFORE UPDATE ON employees
FOR EACH ROW
SET NEW.date_401k_notify = DATE_FORMAT(NEW.date_401k_eligible ,'%Y-%m-%d'),
NEW.date_401k_notify = DATE_SUB(NEW.date_401k_notify,INTERVAL 1 MONTH),
NEW.date_updated = CURDATE()
$$
Delimiter ;
--
Delimiter ;
Delimiter $$
DROP TRIGGER IF EXISTS deleted_employee$$
CREATE TRIGGER deleted_employee BEFORE DELETE ON employees
FOR EACH ROW
INSERT INTO employee_deleted SELECT * FROM employees WHERE id_employee = OLD.id_employee
$$
Delimiter ;
Regards,
Scott.