• <bdo id='ISdxk'></bdo><ul id='ISdxk'></ul>

    <small id='ISdxk'></small><noframes id='ISdxk'>

  1. <i id='ISdxk'><tr id='ISdxk'><dt id='ISdxk'><q id='ISdxk'><span id='ISdxk'><b id='ISdxk'><form id='ISdxk'><ins id='ISdxk'></ins><ul id='ISdxk'></ul><sub id='ISdxk'></sub></form><legend id='ISdxk'></legend><bdo id='ISdxk'><pre id='ISdxk'><center id='ISdxk'></center></pre></bdo></b><th id='ISdxk'></th></span></q></dt></tr></i><div id='ISdxk'><tfoot id='ISdxk'></tfoot><dl id='ISdxk'><fieldset id='ISdxk'></fieldset></dl></div>
    <legend id='ISdxk'><style id='ISdxk'><dir id='ISdxk'><q id='ISdxk'></q></dir></style></legend>

  2. <tfoot id='ISdxk'></tfoot>

      PHP PDO 事务?

      PHP PDO Transactions?(PHP PDO 事务?)

      <i id='JkYeO'><tr id='JkYeO'><dt id='JkYeO'><q id='JkYeO'><span id='JkYeO'><b id='JkYeO'><form id='JkYeO'><ins id='JkYeO'></ins><ul id='JkYeO'></ul><sub id='JkYeO'></sub></form><legend id='JkYeO'></legend><bdo id='JkYeO'><pre id='JkYeO'><center id='JkYeO'></center></pre></bdo></b><th id='JkYeO'></th></span></q></dt></tr></i><div id='JkYeO'><tfoot id='JkYeO'></tfoot><dl id='JkYeO'><fieldset id='JkYeO'></fieldset></dl></div>
            <tbody id='JkYeO'></tbody>

              <legend id='JkYeO'><style id='JkYeO'><dir id='JkYeO'><q id='JkYeO'></q></dir></style></legend>

              • <bdo id='JkYeO'></bdo><ul id='JkYeO'></ul>

              • <small id='JkYeO'></small><noframes id='JkYeO'>

                <tfoot id='JkYeO'></tfoot>
              • 本文介绍了PHP PDO 事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

                I have a signup page and basically I need data inserted into 4 tables. I'm new to PDO and am confused over something.

                基本上,如果任何插入失败,我不希望向数据库添加任何内容,这似乎很简单.

                Basically if any of the inserts fail I don't want anything added to the database, that seems simple enough.

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

                My confusion is, I need to first insert the users username, email, password etc in my users table so I can get (not sure how) using PDO the uid MySQL has given my user (auto incremented by mysql). I need the user uid MySQL gave my user for the other tables as the other tables needs the uid so everything is linked properly together. My tables are InnoDB and I have foreign keys going from users_profiles(user_uid), users_status(user_uid), users_roles(user_uid) to the users.user_uid so they are all linked together.

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

                But at the same time I want to ensure that if for example after data is inserted in the users table (so I can get the uid MySQL gave user) that if any of the other inserts fail that it removes the data that was inserted into the users table.

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

                I thinks it's best I show my code; I have commented out the code and have explained in the code which may make it easier to understand.

                // 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 的新手,上面可能存在我尚未注意到的错误或问题,因为在找出问题之前我无法进行测试.

                I'm new to PDO and there maybe errors or problems above I have yet to notice because I can't test yet until I figure out my problem.

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

                1. I need to know how I can insert the users data in the users table first so i can get the uid MySQL gave my user

                然后获取其他表所需的 uid

                Then get the uid as I need it for the other tables

                但与此同时,如果在插入用户表后查询因任何原因失败,数据也会从用户表中删除.

                But at the same time if a query fails for whatever reason after inserting into users table that the data is also deleted from the users table aswell.

                推荐答案

                该函数返回刚插入记录的主键:PDO::lastInsertIdNEED_USERS_UID_FOR_HERE 参数需要它.在 INSERT 语句之后使用它.

                This function returns primary key of just inserted record: PDO::lastInsertId You will need it for NEED_USERS_UID_FOR_HERE parameter. Use it just after INSERT statement.

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

                Since you started a transaction, data will not be inserted into any table if any error occures provided you use InnoDB engine for your MySQL tables (MyISAM doesn't support transactions).

                这篇关于PHP PDO 事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                  <small id='RgUU0'></small><noframes id='RgUU0'>

                  <i id='RgUU0'><tr id='RgUU0'><dt id='RgUU0'><q id='RgUU0'><span id='RgUU0'><b id='RgUU0'><form id='RgUU0'><ins id='RgUU0'></ins><ul id='RgUU0'></ul><sub id='RgUU0'></sub></form><legend id='RgUU0'></legend><bdo id='RgUU0'><pre id='RgUU0'><center id='RgUU0'></center></pre></bdo></b><th id='RgUU0'></th></span></q></dt></tr></i><div id='RgUU0'><tfoot id='RgUU0'></tfoot><dl id='RgUU0'><fieldset id='RgUU0'></fieldset></dl></div>

                    <legend id='RgUU0'><style id='RgUU0'><dir id='RgUU0'><q id='RgUU0'></q></dir></style></legend>
                      <tfoot id='RgUU0'></tfoot>
                        <tbody id='RgUU0'></tbody>
                        <bdo id='RgUU0'></bdo><ul id='RgUU0'></ul>