Why does my PHP transaction not work?

匿名 (未验证) 提交于 2019-12-03 01:20:02

问题:

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?

Here's the code:

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!";     } }

回答1:

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);

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!

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."); }

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

Unfortunately, not every database supports transactions, so PDO needs to run in what is known as "auto-commit" mode when you first open the connection. Auto-commit mode means that every query that you run has its own implicit transaction, if the database supports it, or no transaction if the database doesn't support transactions. If you need a transaction, you must use the PDO::beginTransaction() method to initiate one. If the underlying driver does not support transactions, a PDOException will be thrown (regardless of your error handling settings: this is always a serious error condition).



回答2:

Your database engine for the tables needs to be INNODB. If you are using phpMyAdmin, it defaults to MyISAM. (I don't know if that will cause the updates not to go through or just the transaction line to be ignored. Edit: Pretty sure the documentation is saying that it will throw an error and not do anything if you beginTransaction on a myISAM)



回答3:

Commit returns TRUE on success or FALSE on failure. You can check it. Also check for errorCode.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!