CodeIgniter CRUD Operations with MySQL
In this tutorial, you can learn CRUD operations in Codeigniter and MySQL. CRUD stands for create, read, update and delete. Create means inserting data into the database using INSERT SQL statement. Read means reading data from database using SELECT SQL statement. Update means updating records using UPDATE SQL query. Finally, Delete means deleting data from the database using DELETE SQL statements. We have covered this tutorial with a live demo to create CRUD operations with Codeigniter and MySQL.
Step 1: Create MySQL Database and Table
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php //Table structure for table `employees` CREATE TABLE `employees` ( `id` int(11) NOT NULL COMMENT 'primary key', `name` varchar(255) NOT NULL COMMENT 'Employee Name', `last_name` varchar(100) DEFAULT NULL, `email` varchar(255) NOT NULL COMMENT 'Email Address', `contact_no` varchar(16) DEFAULT NULL, `address` text, `salary` float(10,2) NOT NULL COMMENT 'employee salary' ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='datatable demo table'; // Dumping data for table `employees` INSERT INTO `employees` (`id`, `name`, `last_name`, `email`, `contact_no`, `address`, `salary`) VALUES (1, 'Nixon', 'Tiger', 'tiger@techarise.com', '9000000001', 'Washington', 3208000.00), (2, 'Garrett', 'Winters', 'winters@techarise.com', '9000000002', 'New York', 170750.00), (3, 'Ashton Cox', 'Ashton', 'cox@techarise.com', '9000000003', 'New Jersey', 86000.00), (4, 'Cedric', 'Kelly', 'kelly@techarise.com', '9000000004', 'Sydney', 433060.00), (5, 'Airi', 'Satouy', 'airi@techarise.com', '9000000005', 'Canberra', 162700.00), (6, 'Brielle', 'Williamson', 'will@techarise.com', '9000000006', 'Wallington', 372000.00), (7, 'Herrod', 'Chandler', 'herrod@techarise.com', '9000000007', 'Germany', 137500.00), (8, 'Rhona', 'Davidson', 'rd@techarise.com', '9000000008', 'Itly', 327900.00), (9, 'Colleen', 'Hurst', 'colleen@techarise.com', '9000000009', 'Moscow City', 205500.00), (10, 'Sonya', 'Frost', 'frost@techarise.com', '9000000010', 'Paris', 103600.00), (11, 'John', 'Philip', 'filip@techarise.com', '9000000011', 'Landon', 26584.00), (12, 'Jaeeme', 'Khan', 'khan@techarise.com', '9000000012', 'New Delhi', 26584.00); // Indexes for table `employees` ALTER TABLE `employees` ADD PRIMARY KEY (`id`); // AUTO_INCREMENT for table `employees` ALTER TABLE `employees` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key', AUTO_INCREMENT=13; ?> |
Step 2: Create a model file
Create a model file named “Curd_model.php” inside “application/models” folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
<?php /** * Description of Curd Model: CodeIgniter CRUD Operations with MySQL * * @author TechArise Team * * @email info@techarise.com */ if (!defined('BASEPATH')) exit('No direct script access allowed'); class Curd_model extends CI_Model { private $_empID; private $_firstName; private $_lastName; private $_email; private $_address; private $_salary; private $_contactNo; public function setEmpID($empID) { $this->_empID = $empID; } public function setFirstName($firstName) { $this->_firstName = $firstName; } public function setLastName($lastName) { $this->_lastName = $lastName; } public function setEmail($email) { $this->_email = $email; } public function setAddress($address) { $this->_address = $address; } public function setSalary($salary) { $this->_salary = $salary; } public function setContactNo($contactNo) { $this->_contactNo = $contactNo; } // get Employee List public function getEmpList() { $this->db->select(array('e.id', 'e.name', 'e.last_name', 'e.email', 'e.address', 'e.contact_no', 'e.salary')); $this->db->from('employees e'); $query = $this->db->get(); return $query->result_array(); } // create new Employee public function createEmp() { $data = array( 'name' => $this->_firstName, 'last_name' => $this->_lastName, 'email' => $this->_email, 'address' => $this->_address, 'contact_no' => $this->_contactNo, 'salary' => $this->_salary, ); $this->db->insert('employees', $data); return $this->db->insert_id(); } // update Employee public function updateEmp() { $data = array( 'name' => $this->_firstName, 'last_name' => $this->_lastName, 'email' => $this->_email, 'address' => $this->_address, 'contact_no' => $this->_contactNo, 'salary' => $this->_salary, ); $this->db->where('id', $this->_empID); $this->db->update('employees', $data); } // for display Employee public function getEmp() { $this->db->select(array('e.id', 'e.name as first_name', 'e.last_name', 'e.email', 'e.address', 'e.contact_no', 'e.salary')); $this->db->from('employees e'); $this->db->where('e.id', $this->_empID); $query = $this->db->get(); return $query->row_array(); } // delete Employee public function deleteEmp() { $this->db->where('id', $this->_empID); $this->db->delete('employees'); } } ?> |
Step 3: Create a controller file
Next create a controller file named “Curd.php” inside “application/controllers” folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
<?php /** * @package Curd : CodeIgniter CRUD Operations with MySQL * * @author TechArise Team * * @email info@techarise.com * * Description of Curd Controller */ if (!defined('BASEPATH')) exit('No direct script access allowed'); class Curd extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('Curd_model', 'emp'); } // Employee list method public function index() { $data['page'] = 'emp-list'; $data['title'] = 'Employee List | TechArise'; $data['empInfo'] = $this->emp->getEmpList(); $this->load->view('emp/index', $data); } // Employee Add method public function add() { $data['page'] = 'emp-add'; $data['title'] = 'Employee Add | TechArise'; $this->load->view('emp/add', $data); } // Employee save method public function save() { $this->load->library('form_validation'); // field name, error message, validation rules $this->form_validation->set_rules('first_name', 'First Name', 'trim|required'); $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required'); $this->form_validation->set_rules('email', 'Your Email', 'trim|required|valid_email'); $this->form_validation->set_rules('address', 'Address', 'trim|required'); $this->form_validation->set_rules('contact_no', 'Phone', 'trim|required'); $this->form_validation->set_rules('salary', 'Salary', 'trim|required'); if($this->form_validation->run() == FALSE) { $this->add(); } else { $first_name = $this->input->post('first_name'); $last_name = $this->input->post('last_name'); $email = $this->input->post('email'); $address = $this->input->post('address'); $contact_no = $this->input->post('contact_no'); $salary = $this->input->post('salary'); $this->emp->setFirstName($first_name); $this->emp->setLastName($last_name); $this->emp->setEmail($email); $this->emp->setAddress($address); $this->emp->setSalary($salary); $this->emp->setContactNo($contact_no); $this->emp->createEmp(); redirect('/'); } } // Employee edit method public function edit($id='') { $data['page'] = 'emp-edit'; $data['title'] = 'Employee Edit | TechArise'; $this->emp->setEmpID($id); $data['empInfo'] = $this->emp->getEmp(); $this->load->view('emp/edit', $data); } // Employee update method public function update() { $this->load->library('form_validation'); // field name, error message, validation rules $this->form_validation->set_rules('first_name', 'First Name', 'trim|required'); $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required'); $this->form_validation->set_rules('email', 'Your Email', 'trim|required|valid_email'); $this->form_validation->set_rules('address', 'Address', 'trim|required'); $this->form_validation->set_rules('contact_no', 'Phone', 'trim|required'); $this->form_validation->set_rules('salary', 'Salary', 'trim|required'); if($this->form_validation->run() == FALSE) { $this->edit(); } else { $emp_id = $this->input->post('emp_id'); $first_name = $this->input->post('first_name'); $last_name = $this->input->post('last_name'); $email = $this->input->post('email'); $address = $this->input->post('address'); $contact_no = $this->input->post('contact_no'); $salary = $this->input->post('salary'); $this->emp->setEmpID($emp_id); $this->emp->setFirstName($first_name); $this->emp->setLastName($last_name); $this->emp->setEmail($email); $this->emp->setAddress($address); $this->emp->setSalary($salary); $this->emp->setContactNo($contact_no); $this->emp->updateEmp(); redirect('/'); } } // Employee display method public function display($id='') { $data['page'] = 'emp-display'; $data['title'] = 'Employee Display | TechArise'; $this->emp->setEmpID($id); $data['empInfo'] = $this->emp->getEmp(); $this->load->view('emp/display', $data); } // Employee display method public function delete($id='') { $this->emp->setEmpID($id); $this->emp->deleteEmp(); redirect('/'); } } ?> |
Step 4: Change Route file
So open “application/config/routes.php” file and add code like as bellow:
1 2 3 4 5 6 7 |
<?php // create routes $route['add'] = 'curd/add'; $route['edit/(:any)'] = 'curd/edit/$1'; $route['display/(:any)'] = 'curd/display/$1'; $route['delete/(:any)'] = 'curd/delete/$1'; ?> |
Step 5: Create a view
Create a view file named “add.php” inside “application/views/emp” folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
<?php $this->load->view('templates/header'); ?> <div class="row"> <div class="col-lg-12"> <h2>CodeIgniter CRUD Operations with MySQL Example</h2> </div> </div><!-- /.row --> <div class="row"> <div class="col-lg-12"> <a href="<?php print site_url();?>" class="pull-right btn btn-primary btn-xs" style="margin: 2px;;"><i class="fa fa-list"></i> List</a> <a href="#" class="pull-right btn btn-info btn-xs" style="margin: 2px;"><i class="fa fa-mail-reply"></i> Back To Tutorial</a> </div> </div><!-- /.row --> <div class="row"> <div class="col-lg-12"> <?php if(validation_errors()) { ?> <div class="alert alert-danger"> <?php echo validation_errors(); ?> </div> <?php } ?> </div> </div> <form action="<?php print site_url();?>curd/save" method="POST" class="add-emp" id="add-emp"> <div class="row"> <div class="col-lg-6"> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-user-o"></i> </span> <input type="text" name="first_name" class="form-control" id="first-name" placeholder="First Name"> </div> </div> </div> <div class="col-lg-6"> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-user-o"></i> </span> <input type="text" name="last_name" class="form-control" id="last-name" placeholder="Last Name"> </div> </div> </div> </div> <div class="row"> <div class="col-lg-6"> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-envelope"></i> </span> <input type="text" name="email" class="form-control" id="email" placeholder="Email"> </div> </div> </div> <div class="col-lg-6"> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-map-marker"></i> </span> <input type="text" name="address" class="form-control" id="address" placeholder="Address"> </div> </div> </div> </div> <div class="row"> <div class="col-lg-6"> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-phone"></i> </span> <input type="text" name="contact_no" class="form-control" id="contact-no" placeholder="Contact No"> </div> </div> </div> <div class="col-lg-6"> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-money"></i> </span> <input type="text" name="salary" class="form-control" id="last-name" placeholder="Salary"> </div> </div> </div> </div> <div class="row"> <div class="col-lg-12 text-right"> <button type="reset" name="reset_add_emp" id="re-submit-emp" class="btn btn-danger"><i class="fa fa-undo"></i> Reset</button> <button type="submit" name="add_emp" id="submit-emp" class="btn btn-primary"><i class="fa fa-save"></i> Submit</button> </div> </div> </form> <?php $this->load->view('templates/footer'); ?> |
Step 6: Create a view
Create a view file named “index.php” inside “application/views/emp” folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?php $this->load->view('templates/header'); ?> <div class="row"> <div class="col-lg-12"> <h2>CodeIgniter CRUD Operations with MySQL Example</h2> </div> </div><!-- /.row --> <div class="row"> <div class="col-lg-12"> <a href="<?php print site_url();?>add" class="pull-right btn btn-primary btn-xs" style="margin-bottom: 5px;"><i class="fa fa-plus"></i> Add Employee</a> </div> </div><!-- /.row --> <div class="row"> <div class="col-lg-12"> <table class="table table-bordered"> <thead> <tr> <th width="15%">First Name</th> <th width="15%">Last Name</th> <th width="15%">Email</th> <th width="10%">Phone</th> <th width="10%">Salary</th> <th width="25%">Action</th> </tr> </thead> <tbody> <?php foreach($empInfo as $key=>$element) { ?> <tr> <td><?php print $element['name']; ?></td> <td><?php print $element['last_name']; ?></td> <td><?php print $element['email']; ?></td> <td><?php print $element['contact_no']; ?></td> <td><?php print $element['salary']; ?></td> <td> <a title="Display" href="<?php print site_url();?>display/<?php print $element['id'];?>" class="btn btn-info btn-xs"><i class="fa fa-eye"></i> </a> <a title="Edit" href="<?php print site_url();?>edit/<?php print $element['id'];?>" class="btn btn-primary btn-xs"><i class="fa fa-edit"></i> </a> <a title="Delete" href="<?php print site_url();?>delete/<?php print $element['id'];?>" onClick="return confirm('Are you sure you want to delete?')" class="btn btn-danger btn-xs"><i class="fa fa-trash"></i></a> </td> </tr> <?php } ?> </tbody> </table> </div> </div><!-- /.row --> <?php $this->load->view('templates/footer'); ?> |
Step 7: Create a view
Create a view file named “edit.php” inside “application/views/emp” folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
<?php $this->load->view('templates/header'); $id = $empInfo['id'] ? $empInfo['id'] : ''; $first_name = $empInfo['first_name'] ? $empInfo['first_name'] : ''; $last_name = $empInfo['last_name'] ? $empInfo['last_name'] : ''; $email = $empInfo['email'] ? $empInfo['email'] : ''; $address = $empInfo['address'] ? $empInfo['address'] : ''; $contact_no = $empInfo['contact_no'] ? $empInfo['contact_no'] : ''; $salary = $empInfo['salary'] ? $empInfo['salary'] : ''; ?> <div class="row"> <div class="col-lg-12"> <h2> CodeIgniter CRUD Operations with MySQL Example</h2> </div> </div><!-- /.row --> <div class="row"> <div class="col-lg-12"> <a href="<?php print site_url();?>" class="pull-right btn btn-primary btn-xs" style="margin: 2px;;"><i class="fa fa-list"></i> List</a> <a href="#" class="pull-right btn btn-info btn-xs" style="margin: 2px;"><i class="fa fa-mail-reply"></i> Back To Tutorial</a> </div> </div><!-- /.row --> <div class="row"> <div class="col-lg-12"> <?php if(validation_errors()) { ?> <div class="alert alert-danger"> <?php echo validation_errors(); ?> </div> <?php } ?> </div> </div> <form action="<?php print site_url();?>curd/update" method="POST" class="edit-emp" id="edit-emp"> <input type="hidden" name="emp_id" value="<?php print $id; ?>"> <div class="row"> <div class="col-lg-6"> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-user-o"></i> </span> <input type="text" name="first_name" class="form-control" id="first-name" placeholder="First Name" value="<?php print $first_name; ?>"> </div> </div> </div> <div class="col-lg-6"> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-user-o"></i> </span> <input type="text" name="last_name" class="form-control" id="last-name" placeholder="Last Name" value="<?php print $last_name; ?>"> </div> </div> </div> </div> <div class="row"> <div class="col-lg-6"> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-envelope"></i> </span> <input type="text" name="email" class="form-control" id="email" placeholder="Email" value="<?php print $email; ?>"> </div> </div> </div> <div class="col-lg-6"> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-map-marker"></i> </span> <input type="text" name="address" class="form-control" id="address" placeholder="Address" value="<?php print $address; ?>"> </div> </div> </div> </div> <div class="row"> <div class="col-lg-6"> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-phone"></i> </span> <input type="text" name="contact_no" class="form-control" id="contact-no" placeholder="Contact No" value="<?php print $contact_no; ?>"> </div> </div> </div> <div class="col-lg-6"> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-money"></i> </span> <input type="text" name="salary" class="form-control" id="last-name" placeholder="Salary" value="<?php print $salary; ?>"> </div> </div> </div> </div> <div class="row"> <div class="col-lg-12 text-right"> <a href="<?php print site_url();?>" id="cancel-emp" class="btn btn-danger"><i class="fa fa-undo"></i> Cancel</a> <button type="submit" name="add_emp" id="submit-emp" class="btn btn-primary"><i class="fa fa-save"></i> Update</button> </div> </div> </form> <?php $this->load->view('templates/footer'); ?> |
Step 8: Create a view
Create a view file named “display.php” inside “application/views/emp” folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php $this->load->view('templates/header'); $first_name = $empInfo['first_name'] ? $empInfo['first_name'] : ''; $last_name = $empInfo['last_name'] ? $empInfo['last_name'] : ''; $email = $empInfo['email'] ? $empInfo['email'] : ''; $address = $empInfo['address'] ? $empInfo['address'] : ''; $contact_no = $empInfo['contact_no'] ? $empInfo['contact_no'] : ''; $salary = $empInfo['salary'] ? $empInfo['salary'] : ''; ?> <div class="row"> <div class="col-lg-12"> <h2>CodeIgniter CRUD Operations with MySQL Example</h2> </div> </div><!-- /.row --> <div class="row"> <div class="col-lg-12"> <a href="<?php print site_url();?>" class="pull-right btn btn-primary btn-xs" style="margin: 2px;;"><i class="fa fa-list"></i> List</a> <a href="#" class="pull-right btn btn-info btn-xs" style="margin: 2px;"><i class="fa fa-mail-reply"></i> Back To Tutorial</a> </div> </div><!-- /.row --> <div class="row"> <div class="col-lg-12"> <p><strong>First Name: </strong><?php print $first_name?></p> <p><strong>Last Name: </strong><?php print $last_name?></p> <p><strong>Email: </strong><?php print $email?></p> <p><strong>Address: </strong><?php print $address?></p> <p><strong>Phone: </strong><?php print $contact_no?></p> <p><strong>Salary: </strong><?php print $salary?></p> </div> </div><!-- /.row --> <?php $this->load->view('templates/footer'); ?> |