insert multi rows and take id from another table

$result = mysql_query(“SELECT * FROM rooms”);
while($row = mysql_fetch_array($result))
{
mysql_query(“insert into chat (name,text,date,chat_id) values(’$_SESSION[username]’,’$_POST[text]’,’$date’,’$row[id]’)”);
}

can i do that without repeat the mysql_query code?

You can do that with one INSERT query (you do not need the SELECT at all):

[php]$sql = sprintf(“INSERT INTO chat (name,text,date,chat_id)
SELECT ‘%s’, ‘%s’, ‘%s’, id FROM rooms”,
$_SESSION[username],
mysql_real_escape_string($_POST[‘text’]),
$date);
mysql_query($sql);[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service