Welcome Guest, Not a member yet? Register   Sign In
Creating a blog with CI with user comments. Having problems, need HELP!
#1

[eluser]Keloo[/eluser]
Hi
I'm trying to create a comment section for my website, and I've got a problem because I cannot insert the comments into the db.
Here is the controller:
Code:
<?php

class Site extends Controller {

    function Site()
    {
       parent::Controller();
      
    }
    
    function index()
    {

      $this->load->library('pagination');
      
      $config['base_url'] = base_url().'index.php/site/index/';
      $config['total_rows'] = $this->db->count_all('posts');
      $config['per_page'] = 2;
      
      $this->pagination->initialize($config);
      
      $data['pagination_links'] = $this->pagination->create_links();

      $this->db->order_by('id','DESC');
      $data['posts_query'] = $this->db->get('posts',$config['per_page'],$this->uri->segment(3));


      $data['main_content'] = 'index';
      $this->load->view('includes/template', $data);

      
    }
  
  function validate_credentials()
  {  
    $this->load->model('users_model');
    $query = $this->users_model->validate();
    
    if($query) // if the user's credentials validated...
    {
      $data = array(
        'username' => $this->input->post('username'),
        'is_logged_in' => true
      );
      $this->session->set_userdata($data);
      redirect('site/index');
    }
    else // incorrect username or password
    {
      $this->index();
    }
  }
function comments()
  {
    $this->db->where('entry_id', $this->uri->segment(3));
    $data['query']  = $this->db->get('comments');
    
    $data['main_content'] = 'post_comments';
    $this->load->view('includes/template', $data);
    $this->is_logged_in();

  }
  
  function comment_insert()
  {
    $this->db->insert('comments', $_POST);
    redirect('site/comments/',$_POST['entry_id']);
  }


  function is_logged_in()
  {
    $is_logged_in = $this->session->userdata('is_logged_in');
    if(!isset($is_logged_in) || $is_logged_in != true)
    {
      echo 'You don\'t have permission to access this page. <a href="../site/index">Login</a>';  
      die();    
      //$this->load->view('login_form');
    }  
  }

And the view :
Code:
<h1>Home</h1>

        &lt;?php if($query->num_rows() > 0): ?&gt;
            
             &lt;?php foreach($query->result() as $row):?&gt;
                 <article class="comments">
                
                  <p class="body">&lt;?=$row->body?&gt;</p>
                  <p>Author: &lt;?=$row->author?&gt;</p>
                 </article>
              <hr />
              
              
             &lt;?php endforeach; ?&gt;
        &lt;?php endif; ?&gt;
        
        
          <p>&lt;?=anchor('site/index', 'Back Home')?&gt;</p>
          
          &lt;?=form_open('site/comment_insert');?&gt;
          
          &lt;?=form_hidden('entry_id', $this->uri->segment(3));?&gt;
          
          <p>&lt;textarea name="body" rows="10" cols="50" value="&lt;?php echo set_value('body'); ?&gt;"&gt;&lt;/textarea></p>
          &lt;?php echo form_error('body','<p class="error">','</p>'); ?&gt;
          
          <p>&lt;input type="text" name="author" value="&lt;?php echo set_value('author'); ?&gt;"/&gt;&lt;/p>
          &lt;?php echo form_error('author','<p class="error">','</p>'); ?&gt;
          
          <p>&lt;input type="submit" value="Post Comment" /&gt;&lt;/p>
          
          &lt;/form&gt;
The problem is when I hit submit it redirects me to something like "http://localahost/mysite/index.php/site/validate_credentials and nothing is inserted into the db.
I really don't know what's wrong here.
I've done something similar with the sign up form and it works perfectly.All the data is inserted into the db.
Here is the model also:
Code:
lass Users_model extends Model {
  
  function validate()
  {
    $this->db->where('username', $this->input->post('username'));
    $this->db->where('password', md5($this->input->post('password')));
    
    $query = $this->db->get('users');
        
    if($query->num_rows == 1)
    {
       return true;
    }
    
  }
  
  function create_user()
  {
    $new_user = array(
     'first_name' => $this->input->post('first_name'),
     'last_name' => $this->input->post('last_name'),
     'username' => $this->input->post('username'),
     'password' => md5($this->input->post('password')),
     'email_address' => $this->input->post('email_address')
    );
    
    $insert = $this->db->insert('users',$new_user);
    return $insert;
  }
I jast want to know where I did wrong, so I can fix this problem. I'm kind of a newbie when it comes to php. I hope I made myself clear enough. If not, just tell me what didn't you understand and I'll try to explain it better.
#2

[eluser]Cristian Gilè[/eluser]
Quote: function comment_insert()
{
$this->db->insert('comments', $_POST);
redirect('site/comments/',$_POST['entry_id']);
}

Check if entry_id, body and author (indexes of the $_POST array) are the right fields names of the comments table.

Change the redirect call to:
Code:
redirect('site/comments/'.$_POST['entry_id']); //dot instead of comma


Cristian Gilè
#3

[eluser]InsiteFX[/eluser]
Also you should not be using $_POST! For security reasons.

Code:
$this->db->insert('comments', $this->input->post('entry_id');
redirect('site/comments/'.$this->input->post('entry_id'));

InsiteFX
#4

[eluser]Keloo[/eluser]
Ok I've fixed it now but I still have some problems because if I put something like: redirect('site/comments'.$this->input->post('entry_id)); it's not working, it's not displaying the ID in the url.
The entry_id it should be the same as the ID of the post.
So here is the controller
Code:
function comments()
  {
    
      $this->load->library('form_validation');
      
      // field name, error message, validation rules
      $this->form_validation->set_rules('body', 'Body', 'trim|required');
      $this->form_validation->set_rules('author', 'Author', 'trim|required');
  
      
      if($this->form_validation->run() == FALSE)
      {
        $this->db->where('id',$this->uri->segment(3));
        $data['posts_query'] = $this->db->get('posts');
        
        $this->db->where('entry_id',$this->uri->segment(3));
        $data['query'] = $this->db->get('comments');
        
        $data['main_content'] = 'post_comments';
        $this->load->view('includes/template',$data);
        
      }
      
      else
      {    
        $this->load->model('users_model');
        
        if($query = $this->users_model->insert_comment())
        {
          $data['main_content'] = 'post_comments';
          $this->load->view('includes/template', $data);
          redirect('site/comments/'.$this->input->post('entry_id'));
        }
        else
        {
          $data['main_content'] = 'post_comments';
          $this->load->view('includes/template',$data);
        
        }
      }

  }

and the model
Code:
function insert_comment()
  {
    $new_comment = array(
     'body' => $this->input->post('body'),
     'author' => $this->input->post('author')

    );
    
    $insert_comment = $this->db->insert('comments',$new_comment);
    return $insert_comment;
  }

I would also like to know how to insert the username instead of the author.
For instance when I'm logged in with a certain username to be able to post a comment with that username instead of typing another name in the author field.
Hope I made myself clear enough, if not feel free to ask me.
#5

[eluser]InsiteFX[/eluser]
You will also need to change your form.
If the username is in your sessions then you should be able to do this:

Code:
function insert_comment()
{
$username = $this->session->userdata('username');

$new_comment = array(
'body' => $this->input->post('body'),
'author' => $username
);

$insert_comment = $this->db->insert('comments',$new_comment);
return $insert_comment;
}

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB