You need transactions to do what you want to. Unfortunately, I don't know if newer versions of MySql support transactions.
You would then do a "commit" after your last insert, or roll back if it fails.
To achieve what you want to do, I would proceed as follows:
- Insert data in the first ("master") table, with an automatically set unique ID.
- get this unique ID (mysql_insert_id() )
- insert data in the other tables, using this unique ID
- delete the records from all tables as soon as a failure occurs (physically [delete from ...] or logically [update ... set deleted=1] delete)
However, there are several issues: You need to assert that the inserts always start from the same table: Assume you have three tables A, B and C. A user inserts data, starting at table A. The unique Id is X.
Another user also inserts data at the same time, starting at table C; his unique ID is (also) X, as it is not yet used in table C
Then, data of user A are being inserted in B (success), then in C (failure, as the unique key is violated).
The second user would prabably fail, too, depending on the rollback method chosen, the order commands are executed etc.
This problem will not happen if you guarantee that in any part of your application, inserts start in table A .
Thisa is only some kind of workaround, I fully agree with you that transactions are the better solution in this case.
JJ Mouris