Delete a record from the Database

After inserting, retrieving, and updating table records, we will now learn how we can delete a particular record from a database table.

To delete a record, first, we have to fetch all the records from a database and show them with the delete link. For this, a delete() function is used to delete a selected record, as shown below:

Syntax

 It has four parameters:

$table: It defines the table name from that a particular record to be deleted

$where: It defines the filed id to be deleted.

$limit: It is used to set the limit in a table.

$reset_data: It determines whether to reset the query.

Use the following method to delete a row from the table employees. The first parameter represents the name of the table from which data will be deleted, and the second parameter tells which record to be deleted.

To delete a record in a database table, we need to create the following files, as shown below:

  1. Employee_controller.php
  2. Data_Model.php 
  3. fetch.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.

Employee_controller.php

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

Data_Model.php

Create a view file fetch.php and save it in application/views/fetch.php. After that, write the following program in the view file.

fetch.php

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.

Suppose in this example; we are going to delete a record no. 5. As we click on the Delete link of the id no.5, it removes the particular record from the list, and it displays the following message, as shown in the below image. 

Delete a record from Database

Furthermore, if we want to check that the record has been successfully deleted from the database, go to phpMyAdmin, and see the table records, as shown in the below image.

Delete a record from Database

Pin It on Pinterest

Share This