2

当我运行这段代码时

<?php

include '../bin/config.php';
connect();

if (isset($_GET['id']) && is_numeric($_GET['id'])){

    $id = $_GET['id'];
    $stmt = $conn->prepare("DELETE FROM noteline WHERE Nid = ?");
    $stmt->bindParam(1, $id, PDO::PARAM_INT);

    $outcome = $stmt->execute();

    if ($outcome){

        echo 'it was successfully deleted';
        header("Location: ../noteline");
    }else {

        echo 'it was not successful due to something';
    }
}


?>

它回显“它已成功删除”但没有从我的数据库中删除任何内容......但是当我通过修改此代码开始事务时:

<?php

include '../bin/config.php';
connect();

if (isset($_GET['id']) && is_numeric($_GET['id'])){

    $id = $_GET['id'];
    $stmt = $conn->prepare("DELETE FROM noteline WHERE Nid = ?");
    $stmt->bindParam(1, $id, PDO::PARAM_INT);

    $conn->beginTransaction();
    $outcome = $stmt->execute();

    if ($outcome){

        $conn->commit();
        echo 'it was successfully deleted';
        header("Location: ../noteline");
    }else {

        echo 'it was not successful due to something';
    }
}


?>

我的数据终于从我的 MySQL 数据库中删除了!我想知道为什么?

4

2 回答 2

2

因为 PDO 连接在禁用自动提交模式的情况下运行。查看该connect()功能以确保您没有禁用此模式。

(另外,我看到您正在使用全局变量来存储连接对象。尽可能避免使用全局变量。)

于 2012-11-09T03:50:58.830 回答
0

听起来您的服务器可能已关闭自动提交?

http://dev.mysql.com/doc/refman/5.0/en/commit.html

默认情况下,MySQL 在启用自动提交模式的情况下运行。这意味着一旦您执行更新(修改)表的语句,MySQL 就会将更新存储在磁盘上以使其永久化。

您的代码中可能有一行发送

SET autocommit=0;

到你的服务器。

于 2012-11-09T03:53:26.413 回答