Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

276
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda