CodeIgniter Database CRUD Tutorial for Beginners with Examples

CRUD is one of the primary functions of a Database Management System. It stands for the four basic operations Create, Read, Update and Delete performed on databases. In SQL, it represents INSERT (Create), SELECT (Read), UPDATE (Update) and DELETE (Delete) commands. Learning the Database CRUD process is essential in application development especially when you want to work with databases. In this codeigniter tutorial, we'll see about building database CRUD operations using CodeIgniter and MySQL Database in detail.

Building Database CRUD Operations in CodeIgniter

To understand CRUD in a simple way, let us consider an example say you develop a web application which handles employee details of a business venture. At the minimum level the application should allow to add (Create), list (Read), update and delete the employee records. So to implement a CRUD process in applications, you should develop some user interface for the user to perform the above said tasks.

Codeigniter follows model-view-controller architecture, and building CRUD process in codeigniter is little tricky with those model, view and controller files. But never mind, this article will take you step by step to create basic crud process using codeigniter and mysql.

CodeIgniter CRUD Example

As an example let us assume that we have to build CRUD for employee’s details. First we'll see how many codeigniter controllers, models and views we require.

Obviously you need one controller file with all the four CRUD functions, one model file for database access and different view files for user interface. The create and update process will share a single view file and another to list the employee details in grid and delete don't require separate view file at all.

The controller for Employee CRUD will look something like this,

<?php
class employeeCRUD extends CI_Controller {

    public function __construct() {
        ...
    }

    function index() {
        ...
    }

    function addEmployee() {
        // database insert code
    }

    function fetchEmployee() {
        // database fetch code
    }

    function updateEmployee() {
        // database update code
    }

    function deleteEmployee() {
        // database delete code
    }
}
?>

Now we'll see all the four crud operations one by one in detail.

Create - Insert Record to Database in CodeIgniter

The create method in database crud is performed on table level and is used to create new entity e.g., on employee table, create will add new employee record. It is accomplished by using the SQL ‘INSERT’ command. To implement this crud process in codeigniter we should create a user interface form with input fields which allows the user to enter the required data and insert into the database. Read the complete tutorial on database insert in codeigniter

Read - Fetch and Display Database Records in CodeIgniter

In the read process we fetch all the records from the database table and list them in a grid view using html table element. This process is accomplished using the SQL ‘SELECT’ command. Read the complete tutorial on read and list the records in codeigniter

Update - Update Database Record in CodeIgniter

The update process is where we edit the details of the existing records in a database. For example we may need to modify the address or salary field of an employee. It can be accomplished by using the SQL ‘UPDATE’ command. To update a particular employee record the 'employee-id' should be given along with the modified details. The employee-id will be the primary key field of a DB table. Read the complete tutorial on updating database record in codeigniter

Delete - Delete Database Record in CodeIgniter

The delete process in database crud is used to delete the existing records in a database. This can be performed on record or table level. This is accomplished by using the SQL command ‘DELETE’ and we should provide an id to delete a particular record in the database. Read the complete tutorial on implementing database delete process in codeigniter

Note: All the above four articles uses the twitter bootstrap css for styling the html forms. Though it is not necessary, the css styles are readily available in bootstrap to jump start coding. But you can omit and use your own style sheets. It will not affect the coding flow.

Must Read: How to Integrate Twitter Bootstrap CSS Framework with CodeIgniter

I hope now you have a better understanding of working with database CRUD process in php codeigniter framework.

1 comment: