php turn array in to mysql queries -


i have form submitted, user created , can assigned number of roles: example (admin, user etc.)

the form posts data array roles[].

when 1 role selectable had 1 mysqli_query() create user (in users table). second create row in roleid table matched userid roleid using mysqls last_insert_id, mysql last_insert_id work if there more 1 role assigned user?

the 3 tables follows:

users (name, email, password, id) roleid (userid, roleid) roles(id, description) 

i need create entry in roleid table each checkbox selected user creation form. thought imploding array (implode(',',$roles)) im not sure mysql accepts values in form. ideas?

you have either query once per role, or insert multiple records in 1 query. mysqli use prepared statements this.

$user_id = mysqli_insert_id(); $query = "insert `roleid` (userid, roleid) values ";  foreach($roles $index => $role_id) {   $roles[$index] = "($user_id," . intval($role_id) . ")"; }  $query .= implode(', ', $roles); 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -