How to insert data using Models in CodeIgniter

2934 Runs 20958 Views 7150 Copies
Saved

Saved

sundip 66

sundip
published 2 years ago

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Controller extends CI_Controller {

/**
 * The index() function gets called automatically when running this 
 * example. It will perform the following steps:
 * 
 *    1) Connect to a mySQL database (see /application/config/database.php)
 *    2) Load a database model (see /application/models/model.php)
 *    3) Create a table named 'items' if it doesn't already exist
 *    4) Insert an item to the 'items's table
 *    5) Retrieves the last inserted item
 *    6) Retrieves the row count of the 'items' table
 *    7) Loads a view and passes the retrieved data
 *
 * The Terminal (adjacent to the Console Output) can be used to connect
 * directly to the mySQL database. Just type in 'mysql' to connect.
 * 
 */


  public function index()
  {
   
    /* Connect to the mySQL database - config values can be found at:
      /application/config/database.php */
    $dbconnect = $this->load->database();


    /* Load the database model:
      /application/models/model.php */
    $this->load->model('model');
    

    /* Create a table if it doesn't exist already */
    $this->model->create_table();
    
    
    /* Call the "insert_item" entry */
    $this->model->insert_item('Hello from Runnable!');

    /* Retrieve the last item  */
    $lastitem = $this->model->get_last_item();

    /* Retrieve the row count */
    $rowcount = $this->model->get_row_count();
    
    $data = array(
                'lastitem' => $lastitem,
                'rowcount' => $rowcount
            );
    
    $this->load->view('view', $data);
    
  }
    
}

/* End of file controller.php */
/* Location: ./application/controllers/controller.php */

Terminal

Your session has timed out.

Dismiss (the page may not function properly).