title: CodeIgniter - adding comments to a post
body: <p>I'd like some help please. I have a post page that has the full post and below the post a small form for adding comments. The uri of the post page is: site/posts/1, so it is in posts controller, and the form action is <code>form_open(site_url('comments/add/'.$post-&gt;post_id))</code>.</p>
  
  <p>This is my add() function inside comments controller:</p>
  
  <pre><code>public function add($post_id){
      // if nothing posted redirect
      if (!$this-&gt;input-&gt;post()) {
          redirect(site_url());
      }
  
      // TODO: save comment in database
      $result = $this-&gt;comment_model-&gt;add($post_id);
      if ($result !== false) {
          redirect('posts/'.$post_id);
      }
  
      // TODO:load the view if required
  }
  </code></pre>
  
  <p>and this is the add() function inside the comment model</p>
  
  <pre><code>public function add($post_id){
      $post_data = array(
          'post_id' =&gt; $post_id, 
          'username'  =&gt; $this-&gt;input-&gt;post('username'),
          'email'     =&gt; $this-&gt;input-&gt;post('email'),
          'comment'   =&gt; $this-&gt;input-&gt;post('comment')
      );
  
      if ($this-&gt;validate($post_data)) {
  
          $this-&gt;db-&gt;insert('comments', $post_data);
  
          if ($this-&gt;db-&gt;affected_rows()) {
              return $this-&gt;db-&gt;insert_id();
          }
          return false;
      } else {
          return false;
      }
  }
  </code></pre>
  
  <p>What I'm trying to do is if the $result = $this->comment_model->add($post_id); fails the validation to display the validation errors in my post view, else insert the comment and redirect to the same post page (site/posts/1). </p>
  
  <p>The problem is that when I hit submit the form action goes in the comments/add/1, as expected, but doesn't do any these above.</p>
  
  <p>Any ideas how can I fix this??</p>
  
  <p><strong>EDIT</strong>
  I did a small change to the code without the 'confusing' validate() function. Maybe this is more helpful.</p>
  
  <p>Comment controller:</p>
  
  <pre><code> public function add($post_id){
      // if nothing posted redirect
      if (!$this-&gt;input-&gt;post()) {
          redirect(site_url());
      }
  
      // TODO: save comment in database
      $this-&gt;form_validation-&gt;set_rules($this-&gt;comment_model-&gt;rules);
    if ($this-&gt;form_validation-&gt;run() == true) {
      echo "Ok! TODO save the comment.";
     // $this-&gt;comment_model-&gt;add($post_id);
     // redirect('posts/'.$post_id);
    } else {
        echo "Validation Failed! TODO: show validation errors!";
    }
  
      // TODO:load the view if required
  }
  </code></pre>
  
  <p>Comment model:</p>
  
  <pre><code> public function add($post_id){
      $post_data = array(
          'post_id' =&gt; $post_id, 
          'username'  =&gt; $this-&gt;input-&gt;post('username'),
          'email'     =&gt; $this-&gt;input-&gt;post('email'),
          'comment'   =&gt; $this-&gt;input-&gt;post('comment')
      );
  
          $this-&gt;db-&gt;insert('comments', $post_data);
  
          if ($this-&gt;db-&gt;affected_rows()) {
              return $this-&gt;db-&gt;insert_id();
          }
          return false;
  } 
  </code></pre>