【问题标题】:PHP PDO prepare transaction statement (Two inserts)PHP PDO 准备事务语句(两个插入)
【发布时间】:2015-10-21 17:17:20
【问题描述】:

我尝试通过事务语句插入两个插入,但没有成功。控制台给我数据库错误。我检查了文档http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers,很明显我遗漏了一些东西。

目标只是将不同的信息插入到两个不同的表中。我尝试了以下方法:

// create record
function create(){

    try {

        $stmt->beginTransaction();

        $query = "INSERT INTO " . $this->table_name . "
                    SET user_id = ?, ";

        // prepare query statement
        $stmt = $this->conn->prepare($query);

        // bind values to be inserted

        $stmt->bindParam(1, $this->user_id);
        $stmt->execute();

        $query2 = "INSERT INTO legalcases_report
                SET user_id = ?, ";

        // prepare query statement 2
        $stmt = $this->conn->prepare($query2);

        $stmt->bindParam(1, $this->user_id);
        $stmt->execute();
        $stmt->commit();
        return true;
    } catch (Exception) {
        $stmt->rollBack();
        return false;
    }
}

【问题讨论】:

  • 你遇到了什么错误?
  • 您从哪里接收值?
  • 来自另一个 php 页面,但数据已经过去了,只有一次插入。我想错误一定来自其他地方。
  • $stmt->execute()
  • 哦,哦,哦,我知道哈哈哈;我是个白痴

标签: php mysql pdo


【解决方案1】:

这段代码有很多问题,希望能全部解决

// create record
function create(){

    try {
        // transaction work on a connection and not a statement
        //$stmt->beginTransaction();
        $this->conn->beginTransaction();


        // Incorrect syntax for an INSERT command
        // Error - Trailing comma in sytax
        $query = "INSERT INTO " . $this->table_name . "
                    SET user_id = ?, ";

        // prepare query statement
        $stmt = $this->conn->prepare($query);

        // bind values to be inserted

        $stmt->bindParam(1, $this->user_id);
        $stmt->execute();


        // Incorrect syntax for an INSERT command
        // Error - Trailing comma in sytax
        $query2 = "INSERT INTO legalcases_report
                SET user_id = ?, ";

        // prepare query statement 2
        $stmt = $this->conn->prepare($query2);

        $stmt->bindParam(1, $this->user_id);
        $stmt->execute();

        // commit also works on a connection object
        //$stmt->commit();
        $this->conn->commit();

        return true;

    // PDO generates a PDOException so you should really catch that,
    // it will fallback to the parent Exception object, BUT
    // there may be times when you want to catch them seperately
    // from the same try block, so use the correct one or both

    } catch (PDOException $pex) {
        $this->con->rollback();
        $pex->getMessage();
        exit; // because you have a serious problem

        // or throw your own exception to the calling code
        throw new Exception('Create user failed ' . $pex->getMessage());
    }

}

Incorrect syntax for an INSERT command

The PHP PDO manual

【讨论】:

    【解决方案2】:

    我猜你应该使用 PDO 对象,而不是 PDOStatement:

    try {
         $this->conn->beginTransaction();
    ...
         $this->conn->commit();
    

    【讨论】:

    • 在到达该行之前大约有 100 个错误
    • 你猜他们应该这样做?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 2010-11-30
    • 2014-04-19
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    相关资源
    最近更新 更多