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_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 ]);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.
¿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