I have a form which is submitted, where a user is created and can be assigned a number of roles: for example (Admin, User etc.)
The form posts the data as an array roles[]
.
When only one role was selectable I had one mysqli_query()
create the user (in the users table). Then the second create a row in the roleid table which matched up a userid with a roleid using mysqls LAST_INSERT_ID
, will the mysql LAST_INSERT_ID
work if there is more than one role assigned to the user?
The three tables are as follows:
users (name, email, password, id)
roleid (userid, roleid)
roles(id, description)
I need to create an entry in the roleid table for each checkbox that is selected by the user creation form. I thought about imploding the array (implode(',',$roles)
) but im not sure mysql accepts values in that form. Any ideas?
LAST_INSERT_ID
in a PHP variable. Then use the variable in your queries. – Rocket 2 days agoINSERT INTO table(field1,field2,field3) VALUES(1,2,3),(4,5,6),(7,8,9)
. – Rocket 2 days agoINSERT INTO roleid (userid, roleid) VALUES(LAST_INSERT_ID(),'1') (LAST_INSERT_ID(),'2')
willLAST_INSERT_ID()
work if its called twice in the same statement? – user1093553 2 days ago