tags:

views:

78

answers:

5

I have two insert statements. The second one will be executed only after successful execution of first one. What I would like to do is:

$sqlone="Insert into .....";
$sqltwo="Insert into.....";

If (mysql_query($sqlone))
{
   If (mysql_query($sqltwo))
   {
      Show message Data inserted in both tables.
   }
}
A: 

That syntax looks like it works, as...

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

http://au2.php.net/mysql_query

Delan Azabani
A: 

From the documentation:

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

So as far as I can see there is no problem with what you already have.

klausbyskov
+1  A: 

Sounds like you're looking for transactions.

A bit of googling gave me some info on database transactions in PHP - hope it helps.

Jeriko
A: 

You can also create the DB transaction as well in which commit of one insert will execute the second insert statement..

OM The Eternity
+2  A: 

Try this

$query1 = '...';
$query2 = '...';
$query3 = '...';
if(mysql_query($query1)) {
     if(mysql_query($query2)) {
          if(mysql_query($query3)) {
              echo "success";
          }
          else { echo "error"; }
     }
     else { echo "error"; }
}
else { echo "error"; }
Starx
Not only did you reiterate what 3 people have already said, you have a typo. `$query3` is opened with a double quote and closed with a single.
anomareh