How to Develop Basic CRUD Operations in CodeIgniter

Today I am going to demonstrate the basic installation of CodeIgniter, a PHP Framework, and a simple example of building a CRUD (Create, Read/Retrieve, Update and Delete) with it.


It’s assumed that you have an intermediate level of understanding of the following:




To work with CodeIgniter, we have to download its latest version (2.1.4 at the time of this writing) from its download link at

WAMP Server Environment

An environment with at least Apache and PHP is needed to work with CodeIgniter. For a Windows platform, WAMP server stack is readily available and its .exe can set up the environment in a few seconds. WAMP server setup can be downloaded from


Once the WAMP server is installed, please unzip the downloaded CodeIgniter file and rename the folder to “cisample,” which will contain the folders for application, system and user_guide, along with a few other files.

Now place this cisample folder inside C:\WAMP\www folder. It’s assumed that the WAMP is installed in its default location. If it is not, then please re-adjust the path accordingly.


In this section, we will configure settings for the database to help build this CRUD sample quickly but with ease of use, reusability and security features. We will also configure some other settings to benefit from the core libraries of this framework.

The first file to setup the configurations is C:\WAMP\www\cisample\application\config\config.php.

Set up the base URL to http://localhost/cisample.

Remove “index.php” from this line’s contents:

like this:

This will make the URLs clean.

What are clean URLs?

To understand this we look at two different examples of URLs:

Normal URL: 

Clean URL:

The Normal URL has a ‘?’ (Query string character) in the URL which is neither user nor search engine friendly. Whereas the Clean URL looks more clean and it is not only user but also search engine friendly. The other benefit of clean URLs is that if the resource providing the functionality for this URL changes, it’s not necessary to change the URL.

For database configuration, first of all create a database in the MySQL database server. The name of the database will be “cisample_db” (without quotation marks). The database hostname, username, password and database are the necessary parameters required to set up the connection between CodeIgniter and the database server. All of the database related settings can be set up inside C:\WAMP\www\cisample\application\config\database.php.

There are a couple of settings needed in C:\WAMP\www\cisample\application\config\autoload.php.

To load the database library, change $autoload[‘libraries’] = array();  to $autoload[‘libraries’] = array(‘database’).

To load the URL and form helpers, change $autoload[‘helper’] = array();  to $autoload[‘herper’] = array(‘url’,’form’).

Auto loading of libraries and helpers makes life easier as one doesn’t have to load the libraries and helpers each and every time when they are needed. They are automatically loaded using the above configurations in the autoload.php file.

Example’s Introduction and Database Table Structure

This example will demonstrate a simple to-do List CRUD. It will also use a simple three column table to hold the to-do items. The name of the columns will be id, title, and detail.

It’s time to dive straight into the code.

1. Start the WAMP environment.
2. Create a table in the database with the following query:

3. Create a controller file “application/controllers/todos.php” for to-dos with the following code:

The index method sets the page title, loads all the to-dos from the model and loads the index view to display the listing of all the to-dos. 

The “view” method gets the to-do ID from the URL, checking to see whether the ID is empty or not. If it’s empty, then it throws a 404 page or gets the to-do item from the model based on the to-do ID and loads “view” to show the to-do item details.

The create method presents the user with a form to create a to-do, processes the input through validation rules and inserts a new record into the database upon form submission.

The edit method does the same as the view method, except it loads the to-do details in a form to edit and afterwards updates the to-do item in the database.

The “complete” method updates the record based on the ID passed in the URL to complete the process. 

4. Create a model file “application/models/todo_model.php” with the following code:

The get_todos method fetches all the to-dos from the database if no ID is passed, or if an ID is passed, it fetches the one to-do item based on that ID.

The set_todos method inserts new data into the database for permanent storage if no ID is passed, or it updates the existing data in the database if an ID is passed.

The completed method marks a record complete in the database.

5. Create the following view files in your application/views/todos/ directory with their respective code:

a) index.php

b) create.php

c) edit.php

d) view.php

e) success.php

6. Insert the following lines in your application/config/routes.php for todos just in case you want the fancy/different URLs from your application resources:

7. Make the following changes in your application/config/config.php for todos:

8. Set the following parameters in your application/config/database.php depending upon your database server settings, mine are set to defaults so:

9. Create a .htaccess file in the cisample directory inside your webroot directory with the following code for URLs like http://localhost/cisample/todos:

10. Now check your Apache web server configuration to make sure that the mod_rewrite module is loaded.

11. Create each of the following files under the application/views/templates directory with its respective code for template:

a) header.php

b) footer.php

12. Load your todos list with http://localhost/cisample/todos.

Making a CRUD using CodeIgniter is both easier and more fun than it seems. CRUD is the first step in learning any new technology where databases are involved. You can move from here to explore more areas and become more comfortable with CodeIgniter. You’ll find those areas of CodeIgniter as easy as this tutorial. Happy CodeIgniting!

The following two tabs change content below.

Khurram K.

PHP Team Lead at Allshore Virtual Staffing
We are a collaborative company at Allshore Virtual Staffing, and encourage our employees to contribute content such as this.Allshore is a remote staffing agency helping startups in the U.S. hire remote software engineers to work as full-time employees. Allshore provides all technical and managerial support via our client services and technology mentorship teams based in Norman, Okla. Contact Allshore today to schedule a free consultation call with a remote staffing specialist or request a free trial of our services.

Latest posts by Khurram K. (see all)

  • WordPress Theming Bootcamp 2016 - May
  • How to Develop Basic CRUD Operations in CodeIgniter - Jun


  1. Hakeem-Reply
    Jun at 1:39 AM

    Nice article Sir!
    Carry on Good Job.

  2. Ramesh-Reply
    Jan at 12:15 PM

    Nice article, Try, where you can generate CRUD operations easily.

  3. sujit-Reply
    Jul at 4:00 PM

    nice codding for seperate parts but still confusing for begginers…

  4. Collins-Reply
    Jul at 4:06 PM

    Beautiful tutorial. Simple and perfect.

  5. Collins-Reply
    Jul at 8:31 AM

    Implemented the entire structure. everything worked for me. except the edit. Edit deosnt work. need your help pls

  6. Manish-Reply
    Oct at 10:55 PM

    Thanks for Nice Post,you can also take reference from

  7. Nov at 3:11 PM

    Simple and clear tutorial. I copied your source codes, and worked no problem in my comp. Thanks.

  8. Apr at 12:02 AM

    We can create auto dynamic crud in codeigniter and other frameworks using

  9. Oct at 7:39 PM

    Thanks for sharing crud concept in very easy way. I m new in codeigniter php. It helps me a lot. I was used it in my website wiki-mobiles

Leave A Comment