Codeigniter – Update

Codeigniter – Update

This tutorial will show you how to edit record and save changes back to database using Codeigniter.

If you are new here, you may want to see

Linux Web Zone CodeIgniter Series

There you can find links for all lessons from our CodeIgniter Series


Controller

We will edit Site.php controller and add new function called edit.

edit function is located after add function from CodeIgniter – Insert tutorial.

Our edit function will look like this

Site.php

 

Additional Settings

Open routes.php file located in application/config directory.

At the end of the file add this line

This will make our link to edit component look like

Instead of

This is optional and if you decide to skip this step, you need to edit Site.php Controller and change this line

To this

This will take the third segment of URL – site / edit / (id)

 

Model

In Component.php Model we need two more functions.

First, we need function that will get a row from database for desired ComponentID.

It’s purpose is to display specific component on our edit_component.php view.

I’ll call it get_row and it looks like this

Create another function and call it update.

It will be used to update row in database with new data from edit form.

update function code is bellow

 

View

On our homepage we will add a link to our controller’s edit function.

site.php

Next, we need a new view for editing component.

It is basically the same as view for adding component but has results from database in form fields.

edit_component.php

 

Results

When we open our homepage, we’ll see Edit option in every row.

codeigniter-update-1

Click on Edit and Edit Component page will open, type another value and click on Update

codeigniter-update-2

After editing component you will be returned to homepage where you can see your edited component

codeigniter-update-3


Thank you for staying with us for yet another lesson of our Linux Web Zone CodeIgniter Series

Stay tuned, soon you’ll see how to delete record from database.

You can download complete source code from this tutorial on the link bellow

CodeIgniter Lesson 3 – Update

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *