CodeIgniter Forums

Full Version: Creating new record in database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I am new in CodeIgniter, I just installed this framework and read documentation. I tried this:

Tutorial. I did two previous sections and everything worked fine, but after finished this one, I try to submit my form and get 404 page.

I noticed that after submiting my form, URL is:

Can it be something wrong with my routes?
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

Here is my News_model

class News_model extends CI_Model {

    public function __construct() {
    public function get_news($slug = FALSE) {
        if ($slug === FALSE)
            $query = $this->db->get('news');
            return $query->result_array();
        $query = $this->db->get_where('news', array('slug' => $slug));
        return $query->row_array();
    public function set_news() {

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

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'text' => $this->input->post('text')

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

My news controller:

class News extends CI_Controller {

       public function __construct()

       public function index()
               $data['news'] = $this->news_model->get_news();
                $data['title'] = 'News archive';

                $this->load->view('templates/header', $data);
                $this->load->view('news/index', $data);

       public function view($slug = NULL){
               $data['news_item'] = $this->news_model->get_news($slug);
                if (empty($data['news_item'])){

                $data['title'] = $data['news_item']['title'];

                $this->load->view('templates/header', $data);
                $this->load->view('news/view', $data);
        public function create() {

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

            $this->form_validation->set_rules('title', 'Title', 'required');
            $this->form_validation->set_rules('text', 'Text', 'required');

            if ($this->form_validation->run() === FALSE)
                $this->load->view('templates/header', $data);


And here is my form:

<h2><?php echo $title; ?></h2>

<?php echo validation_errors(); ?>

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

   <label for="title">Title</label>
   <input type="input" name="title" /><br />

   <label for="text">Text</label>
   <textarea name="text"></textarea><br />

   <input type="submit" name="submit" value="Create news item" />


I really appreciate any help.
Do you have a .htaccess file in the root with index.php ?
Did you set a value for $config['base_url'] in your application/config/config.php file?
(08-11-2016, 05:47 AM)Wouter60 Wrote: [ -> ]Did you set a value for $config['base_url'] in your application/config/config.php file?

It can help me and me with a paging to this script?

Thank you