Views :- 2335

How to insert record in the table using php codeigniter

I last section we saw how to setup codeignitor project. Now we are concentrate on Database side. Let start with insert record example using codeignitor DB classes.


Create Database table trn_employee using MYSQL query browser or sql command

CREATE TABLE  `trn_employee` (
  `emp_id` int(10) unsigned NOT NULL auto_increment,
  `emp_name` varchar(45) collate latin1_general_ci NOT NULL,
  `emp_salary` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`emp_id`)
);


It's time to write dynamic content and start using a database.Setting up your model.First request comes to controller part , then it retrives the input parameters from request which is submitted from GUI. All the parameters are pass to Model part. Model perform the database operation , It insert the record in the database table.


<?php
class EmployeeModel extends Model {

	function EmployeeModel(){
		parent::Model();
		$this->load->library('session');
	}
		
	function insertEmployee($employee){	
		$this->db->insert('trn_employee', $employee);	// insert data into `trn_employee` 
		//table	
	}
}
?>

Setting up your Controller

Lets create new Employee which talk with EmployeeModel class and insert data into table. Here we pass hardcoded values of empoyee rather than taking it from request parameters.

<?php
class Employee extends Controller {

	function Employee(){
		parent::Controller();
		// load the employee model
		$this->load->model('EmployeeModel');
	}

	function index(){
		// create data
		$employee = array(
				'EMP_ID' => 1,
				'EMP_NAME' =>"Sunil",
				'EMP_SALARY' =>18000				
		);
		// table column name should be same as data object key name
		$this->EmployeeModel->insertEmployee($employee); // call the employee model
		$data['EMPLOYEE_DETAILS'] = $employee;
		$this->load->view('employeeresult', $data);
	}

}
?>

Acess the application using url http://localhost/technicaldemo/employee Above Url access the Employee controller index method and insert the hardcoded values into database table

php-codeigniter 

Current Donation 10 $

You also Like this Tutorials

HTML Comment Box is loading comments...

Other Posts

 
Hi I am Yashwant founder of www.technicalkeeda.com, Purpose of this website to share the programming knowledge in the form post , blogs and articles.
 

| Rss Feed | Contact Us | Find us on Google+