SET @last_id = LAST_INSERT_ID();

Posted under » MySQL on 7 Aug 2020

When we insert a record and id is automatically generated. This id may not be the same for all tables, so this method will take out the guessing of the id a thing of the past.

Set @ variable and then use it in the next command.

INSERT INTO `permissions` (`id`, `plugin`, `controller`, `action`, `permission_object_id`, 
 `is_public`, `created`, `modified`) VALUES (NULL, 'users', 'users', 'getsearchusers', '1',
 '0', current_timestamp(), current_timestamp());
SET @last_id_in_table1 = LAST_INSERT_ID();
INSERT INTO `roles_permissions` (`role_id`, `permission_id`, `created`, `modified`) VALUES 
 ('1', @last_id_in_table1, current_timestamp(), current_timestamp()), ('2',
 @last_id_in_table1, current_timestamp(), current_timestamp());

web security linux ubuntu GIT svn Raspberry apache mysql php drupal cake symfony javascript Ajax css