Codeigniter CRUD: Day 4 Update Operation

Codeigniter | Crud | Mvc

Codeigniter CRUD Day 4


This entry is part 4 of 6 in the series CodeIgniter CRUD

Codeigniter CRUD: Day 4 Update Operation: In Day 3 Read Operation we created view & controller for read operation to work. which is responsible for displaying all records from posts table via post model. Now we have our read & create operation ready let’s move on to update Operation of Codeigniter CRUD. So that we can update old entries for posts table.

Add Model Functionality for Codeigniter CRUD’s update operation

Create a new method in post model for fetching single record based on id field from database. We will need it to show single record for editing. Name it getById and write following code in it.

Inside method getById() we set where condition for active record query. Then fetch using get method so only matching records will be returned. Since we know only one record will return from database. Because id field is primary key. so instead of fetching multiple records with result function. we used row() function of active record resource object which is $q. If it fails to match then we will return false.

Controller Code for Codeigniter CRUD’s Update Operation

Next step is to add a method in post controller. name it edit(). Add following code in it.

First we fetch id which will come in URL for edit action as we set in listing page of read operation for Codeigniter CRUD. Then we call model function getById which is created in previous step. if return data is false then we will redirect user to post listing page. If that’s not the case then we will set post variable and pass it to load view method.

Create View for Edit Operation of Codeigniter CRUD

Create a new file in views/post directory. Name it edit.php. Add following code in view file.

This is basically similar to create form we created in CodeIgniter CRUD: Day 2 Create Operation. Just we fill it with data instead of blank textboxes. Because for edit we will need pre-filled data for post record that is being edited. Here if user changes post record and click on update post, all data will be posted on same URL because we have not specified action for form. We can handle it in same method or we can set action of form and handle on different controller method. We will handle it on same method with leaving action blank for form.

Update Post Controller to handle update process

Add below highlighted code in post controller’s edit method.

We checked for post value update_post and it it’s set then we will handle post request and redirect user to listing page after saving changes. First fetch post data from post array. Next we pass that data to update method of post model. We set flash message and redirect user to listing page.

So now we done with update process. let’s run it in browser to see how it works. Navigate to http://localhost::8888/ci/index.php/post  assuming codeigniter project is running on  http://localhost:8888/ci . Click on any record’s edit link under actions column. You will get output just like below screenshot.

Codeigniter CRUD Edit Operation Output

Change data and click on update and you will redirected to post listing page with flash message indicating “Post Updated Successfully”. Please download whole code till update operation of Codeigniter CRUD from below link.


Download Source Code Files


Next Tutorial we will learn how to perform Delete operation of Codeigniter CRUD. Like our Facebook Fan page for instant updates on Tutorials.

Series Navigation<< Codeigniter CRUD: Day 3 Read OperationCodeigniter CRUD: Day 5 Delete Operation >>

Bookmark Link using: bookmark at folkd