How to delete data in CodeIgniter

How to delete data in CodeIgniter

In this example we are going to show you how to delete data from database using CodeIgniter framework in PHP.

Contents

  • Download Codeigniter 3
  • Create a new Database
  • Create a new table
  • Create a new project name
  • Create a new controller file
  • Create a new model file
  • Create a new view file

The DELETE statement is used to delete records from a table:

Following Steps:

  • Create a new table in Mysql database
  • Project folder name is codeIgniter 
  • Database name is sample
  • Create new controller file in Path: codeIgniter\application\controllers\Home.php
  • Create new view file in Path: codeIgniter\application\views\list_data.php
  • Create new model file in Path: codeIgniter\application\models\Home_model.php

1. For creating table the SQL query is:

CREATE TABLE crud (      

  `id` int(11) AUTO_INCREMENT PRIMARY KEY NOT NULL,

  `first_name` varchar(30) NOT NULL,

  `last_name` varchar(30) NOT NULL,

  `email` varchar(30) NOT NULL,

  `mobile` varchar(30) NOT NULL

);

2. Home.php (Controller)

<?php

class Home extends CI_Controller
{

  public function __construct()
  {
    /*call CodeIgniter's default Constructor*/
    parent::__construct();
    /*load database libray manually*/
    $this->load->database();
    /*load Model*/
    $this->load->model('Home_model');

  }

  public function list_data()
  {

    $result['data'] = $this->Home_model->display_records();

    $this->load->view('display_records', $result);

  }

  /*Delete Record*/

  public function deletedata()
  {

    $id = $this->input->get('id');
    $response = $this->Home_model->deleterecords($id);
    if ($response == true) {
      echo "Data deleted successfully !";

    } else {
      echo "Error !";

    }

  }



}

?>

3. list_data.php (View) 

<!DOCTYPE html>

<html>

<head>

  <title>Delete Data</title>

</head>



<body>

  <table width="600" border="1" cellspacing="5" cellpadding="5">

    <tr style="background:#CCC">

      <th>Sr No</th>

      <th>First_name</th>

      <th>Last_name</th>

      <th>Email Id</th>

      <th>Delete</th>

    </tr>

    <?php

    $i = 1;

    foreach ($data as $row) {

      echo "<tr>";

      echo "<td>" . $i . "</td>";

      echo "<td>" . $row->first_name . "</td>";

      echo "<td>" . $row->last_name . "</td>";

      echo "<td>" . $row->email . "</td>";

      echo "<td>" . $row->mobile . "</td>";

      echo "<td><a href='deletedata?id=" . $row->id . "'>Delete</a></td>";

      echo "</tr>";

      $i++;

    }

    ?>

  </table>



</body>

</html>

4.  Home_model.php (Model)

<?php

class Home_model extends CI_Model
{

  /*Display*/

  public function display_records()
  {

    $query = $this->db->get("crud");

    return $query->result();

  }

  public function deleterecords($id)
  {

    $this->db->where("id", $id);

    $this->db->delete("crud");

    return true;

  }

}

?>

Finally enter this url in your browser : http://localhost/codeIgniter/index.php/Home/list_data

We hope this guide has been helpful. If you have any questions or need further assistance, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

Post a Comment

0 Comments