PDA

View Full Version : 2 mysql INSERT statements at same time?



petermdenton
August 23rd, 2007, 01:04 PM
Hello,
I would like to take one post and insert it in 2 differet tables in mysql.

I thought this would work, but it either inserts the first one or the second one. Can anyone help?

$sql="INSERT INTO `da_polls` (`Title`, `Description`, `Usrid`, `Group_ID`, `Value_1`, `Value_2`, `Value_3`, `Value_4`, `Value_5`, `Time`)
VALUES ('$_POST[Title]','$_POST[Description]','$_POST[Usrid]','$_POST[Group_ID]','$_POST[Value_1]','$_POST[Value_2]','$_POST[Value_3]','$_POST[Value_4]','$_POST[Value_5]',NOW())";

$sql="INSERT INTO `da_conversations` (`Title`, `Description`, `Usrid`, `groupid`, `Time`)
VALUES ('$_POST[Title]','$_POST[Description]','$_POST[Usrid]','$_POST[Group_ID]',NOW())";

rschoenbach
August 23rd, 2007, 01:11 PM
do this:

$sql[] = $sql="INSERT INTO `da_polls` (`Title`, `Description`, `Usrid`, `Group_ID`, `Value_1`, `Value_2`, `Value_3`, `Value_4`, `Value_5`, `Time`)
VALUES ('$_POST[Title]','$_POST[Description]','$_POST[Usrid]','$_POST[Group_ID]','$_POST[Value_1]','$_POST[Value_2]','$_POST[Value_3]','$_POST[Value_4]','$_POST[Value_5]',NOW())";

$sql[]="INSERT INTO `da_conversations` (`Title`, `Description`, `Usrid`, `groupid`, `Time`)
VALUES ('$_POST[Title]','$_POST[Description]','$_POST[Usrid]','$_POST[Group_ID]',NOW())";


foreach ($sql as $query) {
mysql_result($query);
}

foodpk
August 23rd, 2007, 06:35 PM
Why are you inserting one post into two tables? It seems to me like your database isn't nearly normalized enough.
Anyway never, ever insert stuff from $_POST (or any other user input) directly into your query, always check that it's safe first.