$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?

Member Avatar for diafol

Yes, you just build up a 'values' string and execute a single query:

$result = mysql_query("SELECT * FROM rooms");
while($row = mysql_fetch_array($result)){
  $r[] = "('" . mysql_real_escape_string($_SESSION['username']) . "','" . mysql_real_escape_string($_POST['text']) . "','" . $date . "','" . $row['id'] . "')";
}
$valuestring = implode(",",$r);
$insert = mysql_query("INSERT INTO `chat` (`name`,`text`,`date`,`chat_id`) VALUES ($valuestring);

Not tested

Yes, you just build up a 'values' string and execute a single query:

$result = mysql_query("SELECT * FROM rooms");
while($row = mysql_fetch_array($result)){
  $r[] = "('" . mysql_real_escape_string($_SESSION['username']) . "','" . mysql_real_escape_string($_POST['text']) . "','" . $date . "','" . $row['id'] . "')";
}
$valuestring = implode(",",$r);
$insert = mysql_query("INSERT INTO `chat` (`name`,`text`,`date`,`chat_id`) VALUES ($valuestring);

Not tested

its doesn't work thank u for your trying ,, i really desperate

Yes, you just build up a 'values' string and execute a single query:

$result = mysql_query("SELECT * FROM rooms");
while($row = mysql_fetch_array($result)){
  $r[] = "('" . mysql_real_escape_string($_SESSION['username']) . "','" . mysql_real_escape_string($_POST['text']) . "','" . $date . "','" . $row['id'] . "')";
}
$valuestring = implode(",",$r);
$insert = mysql_query("INSERT INTO `chat` (`name`,`text`,`date`,`chat_id`) VALUES ($valuestring);

Not tested

omg i fix the code it will be like this


$result = mysql_query("SELECT * FROM rooms");
while($row = mysql_fetch_array($result)){
$r[] = "('" . mysql_real_escape_string($_SESSION) . "','" . mysql_real_escape_string($_POST) . "','" . $date . "','" . $row . "')";
}
$valuestring = implode(",",$r);
mysql_query("INSERT INTO `chat` (`name`,`text`,`date`,`chat_id`) VALUES $valuestring");


thank u a lot its working

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.