Monday, 9 March 2015

how to insert data into mysql using CodeIgniter ?

(1)first go to  application/config folder


and change your connection values:

$db['default']['hostname'] = 'localhost';


$db['default']['username'] = 'root';


$db['default']['password'] = '';


$db['default']['database'] = 'code';


$db['default']['dbdriver'] = 'mysql';


(2)create table 'items':

create table  items
(name varchar(200));


(3)write code for model class & save it as  EmployeeModel.php under "Model "   folder:


<?php

class EmployeeModel extends CI_Model  {



function insertEmployee($employee){
$this->db->insert('items', $employee); // insert data into `trn_employee`
//table
}
}
?>

(4)write code for controller class & save it as  Employee.php under "controllers"   folder :

 <?php

class Employee extends CI_controller {



function index()

{



$this->load->database();



$this->load->model('EmployeeModel');

// create data
$employee = array(

'name' =>"Sunil",

);

// table column name should be same as data object key name

$this->EmployeeModel->insertEmployee($employee); // call the employee model


}


}


?>

No comments:

Post a Comment