I am trying to create a procedure (called a routine in phpMyAdmin) and currently have this query:
BEGIN
INSERT INTO `users` (`username`, `password`, `email`, `user_role_id`,` date_registered`)
VALUES (_username, _password, _email, (SELECT `ID` FROM `roles` WHERE `role_name` = _role) , _date);
END;
But this does not work, I have tried to declare a variable to a role_ID and this also did not work, can anyone help me to fix this please?
NOTE
The params you see (starting with an underscore (_
)) are all IN params used in the phpMyAdmin routine creator
EDIT
The other code I have tried is this:
BEGIN
DECLARE role_id AS/*=*/ (SELECT `ID` FROM `roles` WHERE `role_name` = _role); /* tried both AS and = */
INSERT INTO `users` (`username`, `password`, `email`, `user_role_id`, `date_registered`)
VALUES (_username, _password, _email, role_id, _date);
END;
Also noticed a small error with the DECLARE
, changed the above code to:
BEGIN
DECLARE @role_id ;
SET @role_id = (SELECT `ID` FROM `roles` WHERE `role_name` = _role);
INSERT INTO `users` (`username`, `password`, `email`, `user_role_id`, `date_registered`)
VALUES (_username, _password, _email, @role_id, _date);
END;
But still no luck