dss087358 2012-03-21 09:35
浏览 20

mysql undo查询或功能

Well basically I'm inserting into a relational MySQL database using a MYISAM collation with php.

Lets say a user fills out a web form and posts it for insertion, all the data they provide may belong to parts of several tables.

So using php I'll insert the values into one table, then into another, then into another, etc etc...

But say I insert 3 lots of data into 3 tables, but on the 4th insertion... the sql fails... I then need to return an error message to the user, but ALSO I have to undo all the last inserts.

I could simply just delete all past inserts on fail...

However I wondered if there was an easier way??

Somehow providing the sql queries to mysql engine which temporarily stores the data and SQL, and on command, it runs through all the statements?

Any Ideas?

  • 写回答

4条回答 默认 最新

  • drgaeqdqiiyg14608 2012-03-21 09:37
    关注

    Start with:

    mysql_query("start transaction");
    

    Then, if all of your inserts work successfully:

    mysql_query("commit");
    

    Otherwise, if there is a failure somewhere...

    mysql_query("rollback");
    

    Done ^_^ I love this feature.

    EDIT (following point made in a comment): This will only work in a database engine supporting transactions, and MyISAM is not one of them. I strongly recommend InnoDB, as it supports row-level locking, making your queries much less likely to encounter a lockup.

    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题