6 Steps to Create Simple CRUD Application in CodeIgniter

We have already seen CRUD Application in PHP & MySQL. This is a simple CRUD application in CodeIgniter, if you are new to code igniter and want to learn more this is the place you should start learning. You should know CRUD Operations in Code Igniter, then only you can build any kind of application in Code Igniter.

What is Code Igniter? Code Igniter is a Powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications.

1. Download Code Igniter & Setup

First of all download Code Igniter from codeigniter.com website, I’m using Code Igniter 3.1.5 for this CRUD application. This is the latest version available. Extract this package, place these files on your server. Update htaccess file with this below code to remove index.php from URL, By default the index.php file, will be included in your URLs.

Open config.php file from application->config->config.php and update base URL with the path where you have uploaded. In my case I’ve uploaded to http://localhost/crud-application/, in your case it may be different.

Remove index.php from below line of code and make it blank.
From this code

To this code

2. Create Database, table & Authenticate in CodeIgniter

We are going to work on application directory. In this directory, we will create all the necessary files for models views and controllers. Next, we need to configure the database in CodeIgniter.

Before that create database and database table, below is the SQL code of database table.

In database.php file update the database user name, password & database name. You can find database.php file in application-> config-> database.php

3. Create Operation in Code Igniter

In create operation we will insert the submitted form data in the database. CodeIgniter is an MVC framework (Model, View, Controller), that means for any operation we need to have all these three model, view & controller.

View: I’ll create a directory under view with the name of crud, inside crud directory I’ll create create.php file with form HTML.

Controller: Next I’ll create a controller with the file name of Crud.php under controllers directory. This file is used for all the controllers in our CRUD application.

Model: I’ll create a model with the file name of Crud_model.php under models directory. This model is used to communicate with the database.

We have necessary files created. I’ll add code to files accordingly.

Paste this below code in create.php under views

Now I’m going to work on Crud controller Crud.php under controllers directory. After creating crud controller you will get this code if you are using PHP IDE. If not add this code.

And within our crud controller, I’m creating a constructor to load URL helper & crud model. Add this below code in crud controller.

I’m creating three methods in crud controller, index is default it loads welcome message. Create method loads our create.php view file. create_post method to submit create form data.

If you try to load create method in browser by calling -> http://localhost/crud-application/crud/create. You can see the form to submit data.

Before continuing to submit form data, we have to add action to our form opening tag in create.php view file.

In create post method, I’ll assign all the input field values to variables. Then we will pass this information to crud model.

We have all the variables, we have to pass this information to crud model. Before passing information, I’ll create a method to insert the submitted data in crud model.

Use this code in Crud_model.php file. I’m also creating a constructor to load database.

After creating constructor use this code.

In create post method, add this code after the variables. In this code, I’m redirecting use to read controller if the query is successful. Otherwise, I’m sending the user to failure page.

Add this create failed controller right below the create post controller.

Next, I’ll work on creating read controller.

4. Read Operation in Code Igniter

In Read Operation, first of all, we need to create view.php file in view. This file is used to display all the data from database table.

Use this code to create view.php file

After creating view.php view, I’m going to create read controller this controller helps to fetch the data from database using crud model and display it in our view.php file.

Add this read model in crud model.php file

Replace tbody in view.php file with this below code

In the above code, I’ve already added links for edit and delete, which I’m going to write code in next steps for the update and delete operations.

5. Update Operation in Code Igniter

In update operation, first I’ll fetch the data from database using id passed in URL and this information will be displayed in the form. If the user wants to update any of the fields, the user will edit it and submits the update form.

Here is update controller code to display edit HTML form and also display data using model getting the data.

get record by id model code paste it in crud_model.php file

Copy and paste the create.php view file code in update.php view file. After that update the form code with below form code.

If the update is success full user will be redirected to read view otherwise users will be redirected to update failed where we will display the failure message.

6. Delete Operation in Code Igniter

I’ve already created a delete link in veiw.php file. For delete, we don’t have to create a view, because it’s not required. Through controller, we will delete the record.

We will pass this id to delete method in crud model. There we will delete the record from the database table.

If the delete is a success, then the user will be redirected to read controller. Otherwise, the user will be redirected to delete failed controller. It will display the failure message.

Complete code of CRUD Application in Code Igniter

If you have a problem arranging above pieces of code, you can use this complete code.

Conclusion of CRUD Application in Code Igniter

Till now we have developed complete CRUD Application by using CRUD operations. If you are facing any issues, let me know thorough comment form below.


Sivannarayana Naripeddi how to create database automatically generate username password Code to create database in terminal small registration form in php cart php?ID=iphone 7 shop best free IDEs for PHP development coding for cart in php whois domain checker php script create blog system with crud using php mysql create Registration Page using php simple coding
Vivek Vengala
 

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: