Can't update or delete dynamically with twig and php oop

☆樱花仙子☆ 提交于 2019-11-28 14:34:42

Just adding the ID in my Post object and everything is back in order

  $post = new Post([
  'id' => $_POST['id'],
  'title' => $_POST['title'],
  'header' => $_POST['header'],
  'author' => $_POST['author'],
  'date' => date("Y-m-d H:i:s"),
  'content' => $_POST['content'],
  'featuredImg' => $image
]);

The problem doesn't seem to be involved on the surface of the code you posted however by placing your prepare() statement in a try-catch block, you'll be able to find out if its either an issue in your SQL query or PHP code....

try {
     //...
     $q = $this->_db->prepare('UPDATE posts SET title = :title, featuredImg = :featuredImg, content = :content, author = :author, date = :date, header = :header WHERE id = :id');
     //...
     $q->execute();
} catch (Exception $e) {
     echo "Problem happened: " . $e->getMessage() 
}

This way you can get more then a NULL as a response.

Smaïne

Is your add function does work with the same system ?

Could try this

public function updatePost() {
    $manager = new PostsManager($this->db);
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $p = new Post($_POST);
        $manager->update($p);
    }
}

Or maybe your id is empty

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