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

278
Views
No se puede actualizar o eliminar dinámicamente con twig y php oop

Estoy aprendiendo php oop y me estoy volviendo loco con esto...

Quiero actualizar mi base de datos con un formulario en mi sitio web.

Creo mi modelo así:

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

y mi método:

 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("Ymd H:i:s"), 'content' => $_POST['content'] ]); $manager->update($p); } }

Cuando var_dump($p) tengo mis datos de mi formulario, pero cuando lo pruebo con mi método $manager->update($p) dice nulo, así que nada cambia en mi base de datos.

No estoy usando Doctrine u otra capa DBAL.

Por favor, ¿puedes decirme qué estoy haciendo mal? Repito, estoy aprendiendo. Muchas gracias

EDITAR :

Mi error, cuando hago var_dump($p) , mi id es null , no es bueno, ¿verdad? :

 $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

Simplemente agregando la ID en mi objeto Post y todo vuelve a estar en orden

 $post = new Post([ 'id' => $_POST['id'], 'title' => $_POST['title'], 'header' => $_POST['header'], 'author' => $_POST['author'], 'date' => date("Ymd H:i:s"), 'content' => $_POST['content'], 'featuredImg' => $image ]);
over 4 years ago · Santiago Trujillo Report

0

El problema no parece estar involucrado en la superficie del código que publicó; sin embargo, al colocar su instrucción prepare() en un bloque try-catch , podrá averiguar si es un problema en su consulta SQL o codigo php....

 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() }

De esta manera, puede obtener más de un NULL como respuesta.

over 4 years ago · Santiago Trujillo Report

0

¿Su función de agregar funciona con el mismo sistema?

Podría intentar esto

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

O tal vez tu identificación está vacía

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!