Home  >  Article  >  Backend Development  >  [Help] Problems encountered when php writes data to mysql

[Help] Problems encountered when php writes data to mysql

WBOY
WBOYOriginal
2016-08-04 09:20:55881browse

There is a requirement in the php project to write data to two tables at the same time!
If you write two sql statements directly in the controller, it is possible that the sql of table A is executed, and the sql of table B is not executed.
How to solve such a problem?
Another question is, because the sql of table A has been executed, and the sql of table B has not been executed, can the data of table A be rolled back from MySQL to the time when it was not added?

Reply content:

There is a requirement in the php project to write data to two tables at the same time!
If you write two sql statements directly in the controller, it is possible that the sql of table A is executed, and the sql of table B is not executed.
How to solve such a problem?
Another question is, because the sql of table A has been executed, and the sql of table B has not been executed, can the data of table A be rolled back from MySQL to the time when it was not added?

Just use transaction processing

Excuse me, are the structures of table a and table b the same?

Triggers, you can also use transaction rollback

To use transactions, the database must support innodb

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn