[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>
<?php if($query->num_rows() > 0): ?>
<?php foreach($query->result() as $row):?>
<article class="comments">
<p class="body"><?=$row->body?></p>
<p>Author: <?=$row->author?></p>
</article>
<hr />
<?php endforeach; ?>
<?php endif; ?>
<p><?=anchor('site/index', 'Back Home')?></p>
<?=form_open('site/comment_insert');?>
<?=form_hidden('entry_id', $this->uri->segment(3));?>
<p><textarea name="body" rows="10" cols="50" value="<?php echo set_value('body'); ?>"></textarea></p>
<?php echo form_error('body','<p class="error">','</p>'); ?>
<p><input type="text" name="author" value="<?php echo set_value('author'); ?>"/></p>
<?php echo form_error('author','<p class="error">','</p>'); ?>
<p><input type="submit" value="Post Comment" /></p>
</form>
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.