How to Delete Data from Database in CodeIgniter

How to Delete Data from Database in CodeIgniter

In this post i will explain how to delete data from mysql database in codeigniter, after inserted data, retrieved data and edit/update data or records from database then next step to delete data or records from database.

It is important for any application how to delete or remove data or record from database, so I will show you how to delete data or records from database using codeIgniter framework.

          If you are familiar with  insert, retrieve and edit/update data from database using codeIgniter otherwsie see my link how to insert data in database using codeIgniter how to retrieve data from database using codeIgniter and how to edit/update data from database using codeIgniter

Follow these steps

1.   First see how to insert data in CodeIgniter

2.   Next see how to retrieve data in codeIgniter after that

3.   Add a function in controller, go to where codeigniter is installed path like C:\xampp\htdoc\CITest\application\controllers and open employee.php and create function whose name is delete_employee.

         public function delete_employee($emp_id)

         {
             $emp_id = $this->db->where('emp_id', $emp_id);
             $this->db->delete('citest_employee');
     redirect('employee/index');
         }

4.   Now open employee_form.php go to where codeigniter is installed path like C:\xampp\htdoc\CITest\application\views (Reference how to insert data in codeigniter) and add some piece of code at body section, code shown below.

          <a href='".site_url('employee/delete_employee/'.$row->emp_id)."'>Delete</a>

view look like shown below.

         <!DOCTYPE html>
         <html>
         <head>
            <title>Insert Employee Records in Database</title>
         </head>
         <body>
           <form action="<?php echo site_url('employee/employee_form'); ?>" method="post">
           <table align="center">
              <tr>
               <td>Name:</td>
               <td><?php echo form_input(array('id'=>'emp_name', 'name'=>'emp_name', 'placeholder' => 'Name', 'size'=>25));?></td>
             </tr>
             <tr>
               <td>Gender:</td>
               <td><?php echo form_input(array('id'=>'emp_gender', 'name'=>'emp_gender', 'placeholder' => 'Gender', 'size'=>25));?></td>
            </tr>
            <tr>
             <td>Email:</td>
             <td><?php echo form_input(array('id'=>'emp_email', 'name'=>'emp_email', 'placeholder' => 'Email', 'size'=>25));?></td>
            </tr>
            <tr>
             <td>Phone:</td>
            <td><?php echo form_input(array('id'=>'emp_phone', 'name'=>'emp_phone', 'placeholder' => 'Phone', 'size'=>25));?></td>
            </tr>
            <tr>
             <td>Address:</td>
             <td><?php echo form_input(array('type'=>'textarea','id'=>'emp_address', 'name'=>'emp_address', 'placeholder' => 'Address', 'size'=>25));?></td>
            </tr>
            <tr>
             <td></td>
             <td><button type="submit" id="employee-submit">ADD</button</td>
            </tr>      
         </table>
      </form>
      <table border="1" align="center" style="margin-top: 35px; padding:10px;">
        <thead>
          <tr>
            <th>Emp Id</th>
            <th>Name</th>
            <th>Gender</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Address</th>
            <th>Action</th>
          </tr>
        </thead>
        <tbody>
          <?php
            foreach ($this->employee_model->getEmployees() as $row)
            {
              echo "<tr>
                          <td>$row->emp_id</td>
                          <td>$row->emp_name</td>
                          <td>$row->emp_gender</td>
                          <td>$row->emp_email</td>
                          <td>$row->emp_phone</td>
                          <td>$row->emp_address</td>
                          <td>
                            <a href='".site_url('employee/edit_employee/'.$row->emp_id)."'>Edit</a>
                            <a href='".site_url('employee/delete_employee/'.$row->emp_id)."'>Delete</a>
                          </td>
                      </tr>"; 
            }
          ?>
        </tbody>
      </table>
   </body>
</html>

6.   Go to browser and type localhost/CITest/employee screen shown below


Delete Data from Database in CodeIgniter

7.   Now Click Delete link after that easily you can see data are deleted also you can verify from database, screen shown below.


Delete Data from Database in CodeIgniter





                             I hope, it helped you to understand, how to delete data in database using CodeIgniter Framework.............Thanks

About MSK TUTORIAL

Hi...I am Md Sahjad Karim and I am Engineering Graduate (B.E in Computer Science and Engineering) working as a Web Developer and write Blog Articles. http://www.msktutorial.com/ was launched on july 2016 to provide free online hints, tips, guides and tutorials related to technology(PHP, CodeIgniter, WordPress, Blogger etc). My primary goal is to provide the good, quality and easily to understand the articles to readers around the world.
    Blogger Comment
    Facebook Comment

2 comments:

Ijas said...

where i the model code?

MSK TUTORIAL said...

hiii IJAS
first read all post like insert/retrieve/edit and update/delete data in codeigniter. read carefully all post one by one also see model employee_model.php function like function getEmployee($emp_id) after that i hope u understand all code..if u faced any problems u can message here....

Post a Comment

Thanks For Visiting Blog...