Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

I'm learning php oop and I'm going crazy with this ...

I want to update my database with a form in my website.

I create my model like this :

public function update(Post $post){

$q = $this->_db->prepare('UPDATE posts SET title = :title, featuredImg = :featuredImg, content = :content, author = :author, date = :date, header = :header WHERE id = :id');

$q->bindValue(':title', $post->title(), PDO::PARAM_STR );
$q->bindValue(':content', $post->content(), PDO::PARAM_STR );
$q->bindValue(':author', $post->author(), PDO::PARAM_STR );
$q->bindValue(':header', $post->header(), PDO::PARAM_STR );
$q->bindValue(':date', $post->date(), PDO::PARAM_STR );
$q->bindValue(':featuredImg', $post->featuredImg(), PDO::PARAM_STR);
$q->bindValue(':id', $post->id(), PDO::PARAM_INT);

$q->execute();

and my method :

public function updatePost(){
        $manager = new PostsManager($this->db);
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $p = new Post([
                          'title' => $_POST['title'],
                          'header' => $_POST['header'],
                          'author' => $_POST['author'],
                          'date' => date("Y-m-d H:i:s"),
                          'content' => $_POST['content']
                          ]);
            $manager->update($p);
        }
    }

When I var_dump($p) I have my datas from my form, but when I try it with my method $manager->update($p) it says null so nothing change in my db.

I'm not using Doctrine or another DBAL layer.

Please can you tell me what I'm doing bad ? I repeat, I'm learning. Thanks a lot

EDIT :

My bad, when I var_dump($p), my id is null, it's not good right? :

$object(Post)[13]
    private '_id' => null    
    private '_title' => string 'Spicy jalapeno enim flank laborum prosciutto' (length=44)
    private '_content' => string 'Commodo shankle t-bone, pork loin occaecat ea andouille shoulder venison sausage chicken ... (length=1573)  
    private '_author' => string 'aaaaaaaaaaa' (length=11)
    private '_date' => null
    private '_header' => string 'Spicy jalapeno ...' (length=190)
    private '_featuredImg' => null_
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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
]);
over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!