Home » PHP23 May 2016

CodeIgniter: Simple Add, Edit, Delete, View – MVC CRUD Application

This article shows how to create a simple CRUD (Create, Read, Update, Delete) application with CodeIgniter. CodeIgniter (CI) is a PHP framework that helps building a full-fledged web application.

This article is a continuation of the basic tutorial present in the official CodeIgniter site. The tutorial had view and add data part. But, it didn’t contain the update and delete part. I have just added update and delete functionality in it.

Here is the step-by-step guide on creating a CRUD (Add/Edit/Delete/Update) application in CodeIgniter.

Create Database and Table

First of all, we create database and table. For this article example, we will be creating database named test. We will be creating a table named news inside the database test.

I will present below the full code of controller, model, and views files. We will be adding, updating, deleting news item to our database table. News item just contains title and text content.

Database Config Settings

After you create your database, you need to specify your database host, database name, database username & database password in a database config file in codeigniter. The file is located at application/config/database.php.

My database host is localhost, database name is test (as created by above sql query), database username is root and database password is root.

Here is the config settings for this example:

File: application/config/database.php



First of all, we need a Controller which processes the input request from user, communicates with Model and then loads appropriate Views file.

In the below Controller class, we have different functions (actions) for different tasks. Like, the index() function is used for listing all news items, create() function is used to add a new news item, edit() function is used to edit the news item, and delete() function is used to delete the news item.



Model class communicates with the database. The main database logic is written here. This class is responsible for interacting with database for data select, insert, update and delete purposes.

In the below Model class, get_news() function fetches/selects news items by $slug name. get_news_by_id() function fetches news by it’s ID. set_news() function either edit or add news item. delete_news() function delets any particular news item.

Template Header


This is a template file common for all pages. We call header in all pages.

Template Footer


This is a template file common for all pages. We call footer in all pages.

Index View


This view file is called by index() function of our Controller class. It lists out all the news item.

Detail View


This view file is called by view() function of our Controller class. It prints a particular news item.

Add View


This view file is called by create() function of our Controller class. It prints a form to add news item.

Edit View


This view file is called by edit() function of our Controller class. It prints a form to edit news item.

Add Success View


This view file is called after a new news item is added to database. This file is called in create() function of our Controller class.

Config Routes


Routing rules are defined here. Routes can either be specified using wildcards or Regular Expressions.

Now, you can access your application as http://your-website.com/news.

When you open your project in browser, you will get something like below image:

Simple CRUD MVC PHP CodeIgniter

Download Source Code from GitHub

Hope this helps. Thanks.


Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • Rahul Dubey

    I have created a table in mysql database using php, and also i can displayed that table on the web form, butnthe thing is that i want to do is, I have six field in my table ie. Sr_no which is auto increment, Process_no, Process Name, Ownership, Sheet Revision_no and Revision Date. Now i want to make a dynamic treeview type in my field Process_no for eg:, i have the value in that field M01, so when i’ll click on M01 then their shuld be a sub-list under it such as M01.1, M01.2,….and so on. and this thing i want in every column of that field.
    i had tried alot but fail to do it.
    if you hav any solution or any code for it then plss help mee, plsssss..
    i’m not a experience candidate, new at php and mysql.

  • nirav

    thank you boss

  • Jess Jaime

    Works perfect ! thanks

  • DerekJR321

    Extremely helpful. Thanks!

  • Squirrel

    You are awesome!!!

  • ramesh

    Thanks a lot Bro

  • Manasi Raval

    how can I add pagination to this ..??

  • Phil

    thanks very much!

  • Caleb Al Shi Fong

    Hi. I copy your code. When I click submit button to add a new news_item, nothing happen. Can you explain to me? I use ci ver 2.2.6. thank you

  • SohailAnjum


  • Fariz Samanha

    can you give any references to testing for codeigniter. for example like phpunit etc?

  • D Krishna

    Hi. tried myself with your code. When I click submit button to add a new news_item, nothing happen. waht mistake made im done codeigniter 3.1.6