2

Why insertion operation is not working, my database is not accepting the data I am trying to insert. I have used autoload config to load database and New_model class.

New_controller.php

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class New_controller extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -  
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index($id)
    {
        //$this->load->model("New_model");
        $data["posts"]=$this->New_model->database($id);

        $this->load->view("New_view",$data);
    }

    public function insert_post()
    {
            $this->New_model->create([  
            'post_title'=>'health is wealth',
            'post_description'=>'as we know health is a gift of nature we should take care of our self',
            'post_author'=>'krishna',
            'post_date'=>'2016-01-19'
            ]);
    }
}
?>

I have used autoload config to load New_model class.everything seems to be fine but my insertion operation is not working

New_model.php
<?php

class New_model extends CI_Model {

    public function database($id)
    {
        //$this->load->database();
        //$sql=$this->db->query("SELECT * FROM posts");

        $this->db->where("id",$id);
        $sql=$this->db->get("posts");

        $result=$sql->result_array();

        return $result;
    }
    public function create($data)
    {
        $this->db->insert("posts",$data);
    }

}
?>
1
  • Are you getting an array within your create function try using print_r Commented Jan 19, 2016 at 6:57

2 Answers 2

1

Try this

Change this to

$this->New_model->create([  
            'post_title'=>'health is wealth',
            'post_description'=>'as we know health is a gift of nature we should take care of our self',
            'post_author'=>'krishna',
            'post_date'=>'2016-01-19'
            ]);

this

$data = array(
        'post_title'=>'health is wealth',
        'post_description'=>'as we know health is a gift of nature we should take care of our self',
        'post_author'=>'krishna',
        'post_date'=>'2016-01-19'
    );

$this->New_model->create($data);
Sign up to request clarification or add additional context in comments.

Comments

1

To insert data you have to create your array as

function insert_post() {
    $datanew = array(
        'post_title' => 'health is wealth',
        'post_description' => 'as we know health is a gift of nature we should take care of our self',
        'post_author' => 'krishna',
        'post_date' => '2016-01-19'
    );
    $this->New_model->create($datanew);
}

Read http://www.codeigniter.com/userguide2/database/queries.html

2 Comments

Check updated!! have you load your model in autoconfig.php file??
use $sql = $this->db->last_query(); to print your query

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.