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_