0

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_
DarkBee
  • 16,592
  • 6
  • 46
  • 58
Nathan Meyer
  • 312
  • 7
  • 18
  • The execute() method should return a boolean about the operation'status. Are you using doctrine (is not clear in your question) or other DBAL layer? – Matteo Apr 29 '17 at 07:34
  • Hi ! No I'm not using doctrine (and I don't know what it is :P) – Nathan Meyer Apr 29 '17 at 11:30
  • Are you passing the existing `id` to your form? Either use a hidden field or the query string – DarkBee Apr 30 '17 at 16:09
  • @DarkBee I've just adding the id in my form and now it's working ... but it is a good practice ? – Nathan Meyer May 02 '17 at 09:24
  • 1
    @NathanMeyer well if you are woried about the user tempering with the ID you should validate the `ID` or store the current `ID` that's being editted in a session. You have to pass the `ID` anyhow, otherwise you're code will never know which article it has to edit as `PHP` is stateless :) – DarkBee May 02 '17 at 12:00

3 Answers3

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
]);
Nathan Meyer
  • 312
  • 7
  • 18
-1

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.

  • Hi, thanks for your answer. I've tried to do this just now, and I still have a `Controller/Controller.php:57:null` when I `var_dump($manager->update($p))` – Nathan Meyer Apr 29 '17 at 12:32
-1

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

DarkBee
  • 16,592
  • 6
  • 46
  • 58
Smaïne
  • 1,374
  • 12
  • 19