basically this is on the end of a dynamic form where the user can add multiple users of however many they want to a group, so i storte all the inputs in an array user[].
I want to query the array values开发者_如何学Go and gain the userid for each then run another query with the user ids, inserting the userids into another table.
I am a beginner so I am getting lost in this. I looked at trying to use a for each method but couldnt get that working so now im trying a while.
here is my code ive been playing around with, i imagine its completely wrong :(
$query = 'SELECT * FROM users_tb WHERE student_number IN('.implode(',', $array).')';
mysql_query($query) or die(mysql_error());
$result=mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
$sql = "INSERT INTO group_assocation_tb (group_id, user_id) VALUES('$group','$row['user_id']')";
mysql_query($sql) or die(mysql_error());
mysql_close();
}
please help? :D
regards
You want to use INSERT SELECT:
INSERT INTO group_association (group_id, user_id)
SELECT ' . $group_id . ', user_id
FROM users_tb WHERE student_number IN('.implode(',', $array).')
You have some extra quotes in the INSERT query, it should look like this:
$sql = "INSERT INTO group_assocation_tb (group_id, user_id) ".
"VALUES('$group','$row[user_id]')";
Other than that, it looks like it should work if $group
is a group id.
精彩评论