问题
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_
回答1:
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
]);
回答2:
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.
回答3:
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
来源:https://stackoverflow.com/questions/43691356/cant-update-or-delete-dynamically-with-twig-and-php-oop