2

I'm capturing information from a single PHP form to be recorded in two tables...

What I want is to have both records written or none at all (i.e., if one goes through and the other fails, remove the one that went through)

I wanted to know if there was a way I can detect a failure (possibly using 'or die'?) of the second query, and use this as a trigger to delete the record entered in the first query (or if there is a better method of achieving this)

thanks.

J Smith
  • 23
  • 3

4 Answers4

1

Look into database transactions.

Crashspeeder
  • 4,291
  • 2
  • 16
  • 21
1

use transaction

Toto
  • 89,455
  • 62
  • 89
  • 125
0

Just save the result in a variable and check it with "if"

$result = mysql_query( 'your query...' );
if( $result ) {
     echo 'Everything went fine';
} else {
     mysql_query( 'do other stuff' );
}
T.Koehn
  • 59
  • 2
0

Take a look at this SO question about transactions.

Also note that to use transactions, your database storage engine needs to support them, you can“t use transactions if your table uses the MyISAM storage engine, you will need something like InnoDB.

Community
  • 1
  • 1
jeroen
  • 91,079
  • 21
  • 114
  • 132