Update a record

In this topic, we will learn how we can update existing records in database tables. To update a record, CodeIgniter provides an update() function used with the set() and where() functions as shown below:

  1. set(): As the name suggests, it is used to set the data to be updated in the database table.


It has three parameters;

$key: It defines the fields name, or key/value pairs to be updated.

$value: It defines the fields value, if $key contain a single field.

$escape: This field defines, whether you want to escape value or an identifier.

  • where(): A where() function is used to determine the record to be updated in a database table.


It has three parameters:

$key: It contains the field names to be updated

$value: It contains the value of the field compared to the name of the key defined.

$escape: Determines whether to avoid values ​​and identifiers.

  • update(): An update() function is used to update a single item received from a database.


            It has four parameters:

            $table: The first parameter defines a table name.

$set (array): It defines an associative array that contains the fields value to be set in the database table.

$where (string): It defines a particular row to update.

$limit (int): It is used to set a limit in a database table.

The following are the records of the employees table, that we must update data using the update() function, as shown in the image below.

To update the records in the database table, we need to create the following files, as shown below:

  1. Employee_controller.php
  2. Data_Model.php 
  3. update_view.php

Create a controller file Employee_controller.php and save it in application/controller/Employee_controller.php. And write the following program in the controller file.


Create a model file Data_Model.php and save it in application/model/Data_Model.php. And write the following program in the model file.


Create a view file update_view.php and save it in application/views/update_view.php. And write the following program in the view file.


To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Employee_controller function; It shows the output as shown below.

Now click on any edit link, it redirects you to the update form, as shown below. Suppose, we clicked on the Edit link of the Id no. 5, it redirects and receives all the details of the Id no.5 in the Employee Update Form, as shown in the below image.

Now update some fields of the Id no5.

For Example:

Ms Maeve to Ms Maeve Wiley,

maeve@gmail.com to maevewiley@gmail.com

After updating the details, click the update button, and then, it displays a successful message, as shown in the image below.

If you want to check, that your details have been updated in your main (Employee_controller) page, click on the See Update link, it redirects you to the main Controller page, as shown below.

And you can also verify the update details in the phpMyAdmin database, as shown in the image below.

Pin It on Pinterest

Share This