user1267382
user1267382

Reputation: 1

Why won't this MySQL trigger execute properly?

I am working on a wordpress installation for a client. An ecommerce plugin he's selected does not integrate well with Wordpress Users. I am trying to insert new records into the plugin's user account system when a new user record is created in wordpress. To me, it seemed that a trigger on the wp_users table copying the user data to the plugin table before insert would do the trick. Simple enough. However, in my testing i can't get the wp_users table trigger to do anything. New User Account creation hangs and prevents data insert to wp_users if the trigger event is Before Insert or After Insert. When Specifying Before Update, it allows data insertion in wp_users but not the Plugin table. At no time has it correctly executed the trigger. Here is my Code:

DROP TRIGGER IF EXISTS  `user_update`//
CREATE TRIGGER `dbname`.`user_update`
AFTER INSERT
ON `wp_users` FOR EACH ROW
BEGIN
INSERT INTO `test_data_dump` (`username`, `password`, `email`, `first_name`, `created_at`) VALUES(user_login, user_pass, user_email, user_nicename, user_registered);
END

Any insight would help immensley.

Upvotes: 0

Views: 663

Answers (1)

CAbbott
CAbbott

Reputation: 8098

The trouble would be with this line. You need to use the word new when you're referencing the newly inserted data:

INSERT INTO 'test_data_dump' ('username', 'password', 'email', 'first_name', 'created_at')
    VALUES (new.user_login, new.user_pass, new.user_email, new.user_nicename, new.user_registered);

EDIT: Assuming all your table columns and names match, this syntax should work:

DROP TRIGGER IF EXISTS user_update;

CREATE TRIGGER user_update AFTER INSERT ON wp_users
FOR EACH ROW BEGIN
    INSERT INTO test_data_dump ('username', 'password', 'email', 'first_name', 'created_at')
    VALUES (new.user_login, new.user_pass, new.user_email, new.user_nicename, new.user_registered); 
END;

Upvotes: 2

Related Questions