I am working on a school project creating a CMS for my portfolio. I am having problems with my update function. I have a feeling that this is due to the way I build my PDO Transaction. My database has a project table, a category table, and an associative table content_category. I can just insert my projects into these tables. What I want to do is insert into the table of my projects, then delete all entries from the content_category table and finally insert the entries of the current category into this associative table to complete the transaction. I get the return statement "Project Update". But the tables are not updated. Any ideas?
Here is 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!"; } }
source share