为什么我的PHP事务不起作用? [英] Why does my PHP transaction not work?

查看:264
本文介绍了为什么我的PHP事务不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个学校项目,为我的投资组合网站创建CMS.我无法让我的更新功能正常工作.我觉得这与我构建PDO事务的方式有关.在我的数据库中,我有一个项目表,类别表和关联的content_category表.我可以将我的项目插入这些表中.我想做的是插入我的项目表中,然后从content_category表中删除所有记录,最后将当前类别记录插入到该关联表中以完成交易.我确实得到了返回声明"Project Updated".但是这些表没有被更新.有任何想法吗?

I'm working on a school project creating a CMS for my portfolio site. I am having trouble getting my update function to work. I have a feeling it has something to do with the way I'm constructing my PDO Transaction. In my database I have a projects table, category table, and the associative content_category table. I'm able to insert my projects into those tables just fine. What I want to do is insert into my projects table then delete all records from the content_category table and finally insert the current category records into that associative table to complete the transaction. I do get my return statement "Project Updated" returned. But the tables aren't being updated. Any ideas anyone?

代码如下:

这是我的Project类中的一个函数.

This is a function in my Project class.

public function update(){
    try {
        $conn = getConnection();
        $conn->beginTransaction();
        $sql = "UPDATE project 
                SET project_title = :title, 
                    project_description = :desc, 
                    project_isFeatured = :feat, 
                    project_mainImage = :image
                WHERE project_id = :id";
        $st = $conn->prepare($sql);
        $st->bindValue(":id", $this->id, PDO::PARAM_INT);
        $st->bindValue(":title", $this->title, PDO::PARAM_STR);
        $st->bindValue(":desc", $this->description, PDO::PARAM_STR);
        $st->bindValue(":feat", $this->isFeatured, PDO::PARAM_BOOL);            
        $st->bindValue(":image", $this->mainImage, PDO::PARAM_INT);
        $st->execute(); 

        $sql = "DELETE from content_category
                WHERE content_id = :id";
        $st = $conn->prepare($sql);
        $st->bindValue("id", $this->id, PDO::PARAM_INT);
        $st->execute();

        $sql = "INSERT into content_category (content_id, cat_id)
                VALUES (?,?)";
        $st = $conn->prepare($sql);

        foreach($this->categories as $key=>$value){
            $st->execute(array(intval($projectID), intval($value)));                                        
        }
        $conn->commit();
        $conn = null;
        return "Project updated";                   
    }
    catch(Exception $e) {
        echo $e->getMessage();
        $conn->rollBack();
        return "Error... Unable to update!";
    }
}

推荐答案

为了确保您没有遇到PDO错误,您应该这样设置PDO错误报告:

In order to make sure you are not encountering a PDO error, you should set the PDO error reporting like this:

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

PDO中有一些功能,例如prepare(),它们会返回false或引发PDOException,具体取决于所设置的错误模式.这样,它将引发异常,并且您肯定会知道自己是否有问题!

There are functions in PDO such as prepare() which will either return false or throw a PDOException depending on what error mode is set. This way, it will throw an exception and you'll definitely know if you are having a problem!

此外,如果您的数据库不支持事务(例如MyISAM),则beginTransaction()函数将返回false.因此,也许在其中添加一个检查,如:

Also, if your database doesn't support transactions (like MyISAM), the beginTransaction() function will return false. So, maybe add a check in there like:

if($conn->beginTransaction()) {
   // Do transaction here
} else {
   echo("Unable to use transactions with this database.");
}

奇怪的是,根据 PHP文档,您会如果您的数据库不支持事务,则会出现异常...

Oddly enough, according to PHP documentation, you would be getting an exception if your database doesn't support transactions...

不幸的是,并非每个数据库都支持事务,因此,当您首次打开连接时,PDO需要以所谓的自动提交"模式运行.自动提交模式意味着您运行的每个查询都有自己的隐式事务(如果数据库支持的话),或者没有事务(如果数据库不支持事务的话).如果需要事务,则必须使用PDO :: beginTransaction()方法来发起事务. 如果基础驱动程序不支持事务,则将引发PDOException(无论您的错误处理设置如何:这始终是严重的错误情况).

这篇关于为什么我的PHP事务不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆