0

trying to insert form values into database using codeigniter but nothing heppens. my form is comment_form.php is like,

<?php echo validation_errors(); ?>
<?php echo form_open('news/comment_form'); ?>

Name<input type="text" name="comment_name"></input><br />
Email<input type="text" name="comment_email"></input><br />
Comment<input type="text" name="comment_body"></input><br />
<input type="submit" name="submit" value="Comment it" ></input>

</form>

here's my controller comments.php

class Comments extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('comment_model');
    }

    public function create_comment()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');


        //$data['title'] = 'Create a news item';

        $this->form_validation->set_rules('comment_name', 'comment_name', 'required');
        $this->form_validation->set_rules('comment_email', 'comment_email', 'required');
        $this->form_validation->set_rules('comment_body', 'comment_body', 'required');

        if ($this->form_validation->run() === FALSE) {
            $this->load->view('templates/header', $data);
            $this->load->view('news/comment_form');
            $this->load->view('templates/footer');
        } else {
            $this->news_model->set_comment();
            $this->load->view('news/success');
        }
    }
}

and this is my model comment_model.php

class Comment_model extends CI_Model
{

    public function __construct()
    {
        $this->load->database();
    }


    public function set_comment()
    {
        //$this->load->helper('url');

        //$slug = url_title($this->input->post('title'), 'dash', TRUE);

        $datac = array(
            'comment_name' => $this->input->post('comment_name'),
            'comment_email' => $this->input->post('comment_email'),
            'comment_body' => $this->input->post('comment_body')
        );

        return $this->db->insert('comments', $datac);
    }
}

the problem is whenever i submitting the form it returns nothing, like nothing happened.please help.

1
  • input doesn't have closing tag. Try removing and submitting again. Commented Oct 23, 2013 at 6:28

5 Answers 5

0

In your comment_form.php change

echo form_open('news/create_comment');

to

echo form_open('comments/create_comment');

Why? Because the first parameter you give to form_open() is the action parameter. It will open a form tag like <form action="bla">. And news/create_comment is not the correct page you want to call. Your controller is named comments, that's why you put comments/create_comment.

In your comments.php change

$this->news_model->set_comment();

to

$this->comment_model->set_comment();

Why? Just simply pointing to the false model. Maybe a copypaste-error?

In your comment_model.php remove

$this->load->database();

And load it in your config.php (in the libraries array, add 'database').

Why? IMHO this is the more proper solution. You're probably gonna use your database pretty often, so why load that everytime?

If you still encounter problems, then we need more information. What exactly is not working. Do some debugging for us. After you call $this->news_model->set_comment() write var_dump($this->db->last_query());

Sign up to request clarification or add additional context in comments.

Comments

0
this->news_model->set_comment()

Should be

this->comment_model->set_comment()

PS: sorry for the formatting. Mobile version doesn't read new lines.

Comments

0

Change :

$this->load->database();
echo form_open('news/comment_form');
$this->news_model->set_comment();

To:

$this->load->library('database');
echo form_open('news/create_comment');
$this->comment_model->set_comment();

It would be better if you load your database library in your autoload.php. Or in your controller's constructor.

Comments

0

In the set_comment function after $this->db->insert('comments', $datac); just echo the query using below command and try running the same query manually in the database, you may come to know the issue.

echo $this->db->last_query();

Comments

0

Get the posted values in controller instead of model and then pass them to model.

Controller function

public function create_comment()
{
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->form_validation->set_rules('comment_name', 'comment_name', 'required');
    $this->form_validation->set_rules('comment_email', 'comment_email', 'required');
    $this->form_validation->set_rules('comment_body', 'comment_body', 'required');

    if ($this->form_validation->run() === FALSE) {
        $this->load->view('templates/header', $data);
        $this->load->view('news/comment_form');
        $this->load->view('templates/footer');
    } else {
        $data['comment_name']   =   $this->input->post('comment_name'),
        $data['comment_email']  =   $this->input->post('comment_email'),
        $data['comment_body']   =   $this->input->post('comment_body')

        $this->news_model->set_comment();
        $this->load->view('news/success');
    }
}

Model function

public function set_comment($data)
{
    //$this->load->helper('url');

    //$slug = url_title($this->input->post('title'), 'dash', TRUE);

    $this->db->insert('comments', $data);

    return $this->db->insert_id();
}

EDITS

The above code will work if you follow these steps.

Go to database.php in application/config and provide database connection settings like hostname , username , password and database name.
Then go to autoload.php in application/config and autoload the database library like this

$autoload['libraries'] = array('database');

Also remove the closing tags for input in your html form.
Try testing like this in controller before moving forward

$post = $this->input->post();
echo '<pre>';
print_r($post);

This will ensure you are receiving post data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.