PHP PDO 交易?

标签 php pdo transactions

我有一个注册页面,基本上我需要将数据插入到 4 个表中。我是 PDO 的新手,对某些事情感到困惑。

基本上,如果任何插入失败,我不想向数据库中添加任何内容,这看起来很简单。

我的困惑是,我需要首先在我的 users 表中插入用户用户名、电子邮件、密码等,这样我就可以(不确定如何)使用 PDO 获取 MySQL 为我的用户提供的 uid(由 mysql 自动递增)。我需要 MySQL 为我的用户提供其他表的用户 uid,因为其他表需要 uid,所以一切都正确链接在一起。我的表是 InnoDB,我有从 users_profiles(user_uid)、users_status(user_uid)、users_roles(user_uid) 到 users.user_uid 的外键,因此它们都链接在一起。

但与此同时,我想确保如果在 users 表中插入数据后(这样我可以获得 MySQL 给用户的 uid),如果任何其他插入失败它删除了插入到 users 表中的数据。

我认为最好展示我的代码;我已经注释掉了代码,并在代码中进行了解释,这样可能更容易理解。

// Begin our transaction, we need to insert data into 4 tables:
// users, users_status, users_roles, users_profiles
// connect to database
$dbh = sql_con();

// begin transaction
$dbh->beginTransaction();

try {

    // this query inserts data into the `users` table
    $stmt = $dbh->prepare('
                        INSERT INTO `users`
                        (users_status, user_login, user_pass, user_email, user_registered)
                        VALUES
                        (?, ?, ?, ?, NOW())');

    $stmt->bindParam(1, $userstatus,     PDO::PARAM_STR);
    $stmt->bindParam(2, $username,       PDO::PARAM_STR);
    $stmt->bindParam(3, $HashedPassword, PDO::PARAM_STR);
    $stmt->bindParam(4, $email,          PDO::PARAM_STR);
    $stmt->execute();

    // get user_uid from insert for use in other tables below
    $lastInsertID = $dbh->lastInsertId();

    // this query inserts data into the `users_status` table
    $stmt = $dbh->prepare('
                        INSERT INTO `users_status`
                        (user_uid, user_activation_key)
                        VALUES
                        (?, ?)');

    $stmt->bindParam(1, $lastInsertID,     PDO::PARAM_STR);
    $stmt->bindParam(2, $activationkey,    PDO::PARAM_STR);
    $stmt->execute();

    // this query inserts data into the `users_roles` table
    $stmt = $dbh->prepare('
                        INSERT INTO `users_roles`
                        (user_uid, user_role)
                        VALUES
                        (?, ?)');

    $stmt->bindParam(1, $lastInsertID,      PDO::PARAM_STR);
    $stmt->bindParam(2, SUBSCRIBER_ROLE,    PDO::PARAM_STR);
    $stmt->execute();

    // this query inserts data into the `users_profiles` table
    $stmt = $dbh->prepare('
                        INSERT INTO `users_profiles`
                        (user_uid)
                        VALUES
                        (?)');

    $stmt->bindParam(1, $lastInsertID,      PDO::PARAM_STR);
    $stmt->execute();

    // commit transaction
    $dbh->commit();

} // any errors from the above database queries will be catched
catch (PDOException $e) {
    // roll back transaction
    $dbh->rollback();
    // log any errors to file
    ExceptionErrorHandler($e);
    require_once($footer_inc);
    exit;
}

我是 PDO 的新手,上面可能存在我尚未注意到的错误或问题,因为在我弄清楚我的问题之前我无法进行测试。

  1. 我需要知道如何先将用户数据插入到用户表中,这样我才能获得 MySQL 给我的用户的 uid

  2. 然后获取其他表所需的 uid

  3. 但与此同时,如果查询在插入用户表后由于某种原因失败,数据也会从用户表中删除。

最佳答案

此函数返回刚刚插入记录的主键:PDO::lastInsertId NEED_USERS_UID_FOR_HERE 参数需要它。在 INSERT 语句之后使用它。

自从你开始一个事务,如果你的 MySQL 表使用 InnoDB 引擎(MyISAM 不支持事务),如果发生任何错误,数据将不会被插入到任何表中。

关于PHP PDO 交易?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10155946/

相关文章:

javascript - document.forms.submit 挂了我的页面

php - 是否可以在 PDOStatement 对象的单个实例上执行多个 fetch() 操作?

sql - Grails 事务(不是基于 GORM,而是使用 Groovy Sql)

java - 提交后是否禁止 begin() 同一事务(事务重用)?

sql-server - ZombieCheck 异常 - 此 SqlTransaction 已完成;它不再可用——在简单提交期间

php使用insert语句添加到数据库

javascript - 无法在javascript中调用同步功能

php - 是否可以计算 PDO 准备语句中的参数数量?

php - 使用用于 MySQL 的 PDO 准备语句将多个值从数据数组插入多个列

php - 如何将类添加到 $_GET 上的侧边栏