1

At the moment I am trying to insert into 4 separate tables using 4 separate INSERT commands.

I only want this to happen if all 4 of the INSERTS are successful. If any of the INSERT's fail then I do not want any of the data to be entered into the DB.

At the moment I have a very ugly looking php IF statement which I'd rather not include here as I'm sure there must be a better way.

David Jack
  • 87
  • 6
  • 3
    it's called a `transaction`: http://www.tutorialspoint.com/sql/sql-transactions.htm and you DEFINITELY don't want to be simulating them with client-side code. highly unreliable and dangerous. – Marc B Jun 07 '16 at 20:04

1 Answers1

3

You should use transaction:

<?php
$conn = mysqli_connect(...)
mysqli_begin_transaction($conn);
//do some inserts
if($insert1 && $insert2 ..) {
    mysqli_commit($conn);
} else {
    mysqli_rollback($conn);
}

Depending on your database configs, you should disable autocommit: mysqli_autocommit($conn, false);

Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29