Example of Add,Edit and Delete Record Using Codeigniter and MySQL

This Codeigniter tutorial will help to create sample crud operation application using ci framework and MySQL, As you know CRUD example will have add record,edit record and delete record from database.I am creating CRUD operation using Codeigniter framework and MySQL database.We will create simple Codeigniter example that will use MySQL for database related operation.

I have already shared Simple Listing of Codeigniter using MySQL. I am extending this tutorial and will add functionality to add,edit and delete record from database.

You can get basic CI configuration information from jQuery datatable Listing Example and template integration using Simple Layout Example in Codeigniter.

CRUD Application Example Using Codeigniter and MySQL

Below is step by step process to create listing, add, edit and delete record using Codeigniter and MySQL, In earlier codeigniter tutorials, I have created connection configuration and enable library into bootstrap file.

I am assuming you have controllers/Home.php file and employee.php model which will use in this tutorials.You can read Simple Listing and configuration of Codeigniter Framework with MySQL.

We can make home controller as default controller like below using application/config/routes.php file.

How to Insert Record into MySQL Using Codeigniter

So now we have HTML table listing and will add feature to add new records into MySQL database.We will create new add view file and bind with action method in controller file.

Step 1: Created add button HTML element into home.php listing page.

Step 2: We will create a new create.php file in views/ folder which will use to enter employee record information to create new record into MySQL.

As you can see, I used /home/create action point in HTML form.We will add create() method into home controller file(controllers/Home.php).

Step 3: We will create a method in controllers/Home.php file which will use to create a new record into MySQL using model method(We will later discuss),

I am using codeigniter form_validation library to validate form data, if everything validate fine then access model saveEmployee() method to save posted data into MySQL database.

Step 4: We will create model saveEmployee() method into m_employee.php file.

I am using same model method for create new entry in employee table as well update existing entry in employee table.

Step 5: We will add new entry in config/routes.php file.

How to Edit or Update records into Codeigniter

We have created form to add new record into MySQL database using codeigniter. We will next step and update existing entry using codeigniter and MySQL. Create a new edit record page in views folder and bind with update controller method.

Step 1: We will add edit action button on table each row as like below,

Step 2: We will create a new edit.php file in views/ folder which will use to update employee record information into MySQL table.

As you can see, I used /home/edit action point in HTML form with record primary id. The url will make like home/edit/1 where 1 is employee id which is primary key in MySQL table.
We will add a new method edit() method into home controller file.

Step 3: We will create a new edit() method in controller/home.php file which will use to update a new record into MySQL using model method,

As stated above,I am using codeigniter form_validation library to validate form data. Validator return true and false, if validator return false then return posted data on edit page with validator message otherwise access model saveEmployee() to save posted data into MySQL database.

I am not creating saveEmployee() method in model, since its already created in model file at the time of create new record step.

Step 4: We will add new entry in config/routes.php file.

How to Delete Record into Codeigniter Using MySQL

We will create delete functionality in this posed tutorial.I do not need to create view for delete record we just create action delete button and attached on each table row.

Step 1: We will attached delete action table row as like below,

Step 2: We will create delete() action method in controller/Home.php file.

first we will check record is existing then delete action will fire.

Step 3: We will create delete() model method in m_employee.php file.

Step 4: We will add new entry in config/routes.php file.

Conclusion:

I have added insert,edit and delete record into MySQL using Codeigniter framework.I used Codeigniter validation, url helper library to validate input values and create urls. I am using simple three columns table.You can create complex form as well using this codeigniter tutorial.

You can download source code from below link.