jeudi 28 octobre 2021

Add a post id to a comment PHP

Newbie there, hello I create a blog and need to associate posts/comments but it doesn't work because article_id stay at 0. comments table In Commentary class:

public function addComm($data){
        $this->dbcomm->query('INSERT INTO comments (user_id, article_id, commentbody) 
                              VALUES(:user_id, :article_id, :commentbody)');
        //Bind values
        $this->dbcomm->bind(':user_id', $data['user_id']);
        $this->dbcomm->bind(':article_id', $data['article_id']); 
        $this->dbcomm->bind(':commentbody', $data['commentbody']);
        

        // Execute
        if($this->dbcomm->execute()){
            return true;
        } else {
            return false;
        }

    }

And function :

public function comment(){
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
            // Sanitize POST array
            $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

            $data = [
            'commentbody' => trim($_POST['commentbody']),
            'user_id' => $_SESSION['user_id'],
            'commentbody_err' => '',
            'article_id' => '',
          ];

          //validate title
          if(empty($data['commentbody'])){
            $data['commentbody_err'] = 'Svp entrer un commentaire';
          }         
          
          //Make sure no errors
          if(empty($data['commentbody_err'])){
            //validated
            if($this->commentModel->addComm($data)){
                flash('post_message', 'Commentaire ajouté');
                redirect('posts/');
            } else {
                die("Quelque chose n'est pas bon");
            }
          } else {
            //load view with errors
            $this->view('posts/comment', $data);
          }
        } else {
          $data = [
            'title' => '',
            'commentbody' => '',
          ];

        $this->view('posts/comment', $data);
        }

}

If I type : 'article_id' => $post->id, it doesn't works.. Can you help me guys please




Aucun commentaire:

Enregistrer un commentaire