0

I am integrating my website with a forum. So basically when a users registers through my main website, i must insert the account information in my users table and the forum's users table. However there is a slight chance, that the first query may fail, thus leaving one table empty of information, while the other has it.

If i can insert the information in both tables with one query, should it fail nothing will be added to the DB

2 Answers 2

2

Read up on transactions. You want to START TRANSACTION, do two INSERTs, and the COMMIT

Sign up to request clarification or add additional context in comments.

2 Comments

Is transactions available in mysql(possibly mysqli as well) and what are the bad points in using it? And does it support auto_increment?
Yes, both transactions and auto-increment are available.
1

Databases know the concept of transactions for such problems.

You encapsulate multiple database statements within a transaction and either all statements are successfull or all actions are rolled back to the previous state.

With PHP and mysqli you can do something like this:

$conn = new mysqli("localhost", "my_user", "my_password", "my_db");

//  turn off autocommit = beginn a transaction
$conn->autocommit(false);

// your db actions: multiple inserts ... what you want
$mysqli->query("insert into table1....");
$mysqli->query("insert into table2....");

// commit all changes and close connection
$conn->commit();
$conn->close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.