Jump to content

Recommended Posts

Hi Everyone,

 

I am trying to create a history trigger so that when I change data in one field it adds to the history table with the old data. 

 

This is what I have so far:

 

CREATE TRIGGER StaffUpdate BEFORE UPDATE ON Staff
    FOR EACH ROW
    BEGIN
        INSERT IGNORE INTO StaffHistory
        (
            StaffID,
            FirstName,
            MiddleName,
            Surname,
            PositionTitle,
            IDNumber,
            Address,
            StreetAddress,
            PostCode,
            City,
            PhoneNumber,
            HomeNumber,
            MobileNumber,
            eMail,
            IRDNumber,
            EmploymentDate,
            RetirementDate,
            Password,
            Username,
            AdminStaff,
            AdminStudent,
            AdminTrainers,
            AdminCourses,
            AdminProviders,
            LastChangedBy,
            LastChangedDate
        )
        VALUES
        (
            OLD.StaffID,
            OLD.FirstName,
            OLD.MiddleName,
            OLD.Surname,
            OLD.PositionTitle,
            OLD.IDNumber,
            OLD.Address,
            OLD.StreetAddress,
            OLD.PostCode,
            OLD.City,
            OLD.PhoneNumber,
            OLD.HomeNumber,
            OLD.MobileNumber,
            OLD.eMail,
            OLD.IRDNumber,
            OLD.EmploymentDate,
            OLD.RetirementDate,
            OLD.Password,
            OLD.Username,
            OLD.AdminStaff,
            OLD.AdminStudent,
            OLD.AdminTrainers,
            OLD.AdminCourses,
            OLD.AdminProviders,
            OLD.LastChangedBy,
            OLD.LastChangedDate
        );
    END

 

At present it does save the last changed date but it doesn't save the data. 

 

If you have any help it would be really appreciated.

 

Regards,

Chris

Link to comment
https://forums.phpfreaks.com/topic/159185-solved-mysql-history-trigger/
Share on other sites

I'm pretty certain you need to replace this line:

 

        INSERT IGNORE INTO StaffHistory

 

with this:

 

        REPLACE INTO StaffHistory

 

REPLACE INTO will either update a record if it's already there, or create (insert) a new record if it doesn't exist.

 

* NOT TESTED - Back up before trying this *

Hi Maq,

 

Thank you for you imput but I have relised that the sql was correct and it was my PHP that was incorrect.  On the PHP output page I had listed it from StaffID rather than StaffHistoryID. My bad!

 

Thanks again for looking though.

 

Chris

 

No problem.  So you have resolved the issue, and everything is working properly now?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.