Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7941631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:43:39+00:00 2026-06-03T23:43:39+00:00

I’ve just learned the MySQL triggers and how they work. I decided to apply

  • 0

I’ve just learned the MySQL triggers and how they work. I decided to apply it on my small website.

I have a Users table where new users accounts are created and I would like to keep a history of adding new accounts in a UsersHistory table.

The error is that when I execute the following query, it gives me an error:

Query:

CREATE TRIGGER User_After_Insert 
AFTER INSERT ON UsersHistory FOR EACH ROW WHEN NOT NEW.Deleted 
BEGIN                  
    SET @changeType = 'DELETE';  

    INSERT INTO UsersHistory (UserID, changeType) 
    VALUES (NEW.ID, @changeType);    
END;

CREATE TRIGGER User_After_Insert1 
AFTER INSERT ON UsersHistory FOR EACH ROW WHEN NEW.Deleted 
BEGIN              
    SET @changeType = 'NEW';  

    INSERT INTO UsersHistory (UserID, changeType) 
    VALUES (NEW.ID, @changeType);        
END;

The error is:

1064 – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘WHEN NOT NEW.Deleted
BEGIN Â
SET @changeType = ‘DELE’ at line 1

I looked for a solution but I couldn’t find.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:43:41+00:00Added an answer on June 3, 2026 at 11:43 pm

    Have you set a DELIMITER to something different than “;”? Also, I saw some stuff I didn’t know to be supported in mysql (the WHEN statements before the BEGIN blocks), so heres my suggestion:

    delimiter //
    CREATE TRIGGER User_After_Insert AFTER INSERT ON UsersHistory FOR EACH ROW
    INSERT INTO UsersHistory (UserID, changeType) VALUES (NEW.ID, 'NEW')//
    CREATE TRIGGER User_After_Delete AFTER DELETE ON UsersHistory FOR EACH ROW              
    INSERT INTO UsersHistory (UserID, changeType) VALUES (OLD.ID, 'DELETE')//
    delimiter ;
    

    UPDATE

    Due to your comment below, I think you need to read up on trigger a bit more mate. But here’s the gist.

    The above statements, create triggers in the actual database. In effect, you “install” the triggers in your database schema. Running the statements in any mysql client, will create the triggers if you have appropriate account rights.

    Now, from this stage on, you dont explicitly call them from PHP or anything like that. They live in the database and are called automatically when you perform certain actions. In the above case, AFTER a record is deleted from UserHistory or AFTER a record is inserted into UserHistory.

    So, when you run “INSERT INTO UserHistory VALUES …” from your php script, the database will fire the trigger automatically.

    Hope that makes sence.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.