codeigniter delete data from database - Web Development and Web Design Codes

Latest

Monday, February 5, 2018

codeigniter delete data from database

Codeigniter delete data from database


codeigniter delete data from database
codeigniter delete data from database

In This lesson we are going to learn how to delete data from data using Codeigniter. Follow the below to learn delete data from data using Codeigniter.
=>Database and Table:
  Database Name: test
  Table Name: user

SQL Codes for Creating Table user:
CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(150) NOT NULL,
  `email` varchar(150) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

SQL for Insert rows into user
INSERT INTO `user` (`id`, `username`, `email`) VALUES
(NULL, 'ANJAN KUMAR', 'Anjankumardhali@gmail.com'),
(NULL, 'ANJAN', 'biswasshiuli608@gmail.com'),
(NULL, 'Supriya Gain', 'SupriyaStar@Yahoo.com'),
(NULL, 'Priya roy', 'priya6@gmail.coma'),
(NULL, 'ANJAN KUMAR', 'Anjankumardhali@gmail.com'),
(NULL, 'ANJAN', 'biswasshiuli608@gmail.com'),
(NULL, 'ANJANBD', 'biswasshiuli608@gmail.com');
//Some Configuration:
Base Url:
go to application/config/config.php and configure look like below:
$config['base_url'] = 'http://localhost/codeigniter/';
Autoload Url:
go to application/config/autoload.php and configure look like below:
$autoload['helper'] = array('url');
Database Configuration:
application/config/database.php

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'test', //Your Database table name here
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

Files:

1. View File: index.php
Codes for index.php (codeigniter/application/views)
<h2>Codeigniter Delete Data</h2>
<table border="1" cellspacing="0">
   <tr>
      <th>Name</th>
   <th>Email</th>
   <th>Action</th>
   </tr>
   <?php
foreach($result as $row){
 ?>
   <tr>
      <td><?php echo $row->username;?></td>
   <td><?php echo $row->email;?></td>
   <td><a href="<?php echo base_url();?>index.php/Blog/delete/<?php echo $row->id;?>">Delete</a></td>
   </tr>
   <?php
}?> 
</table>
2. Controller File: Blog.php
Codes for Blog.php (codeigniter/application/controllers)
<?php
class Blog extends CI_Controller{
 //Loadind Model in Contructor 
 function __construct(){
    parent::__construct();
         $this->load->model('User');
} 

 public function index(){
  $data['result']= $this->User->getuser();
  $this->load->view('index',$data);
 }
 public function delete(){
  //Getting ID from Index page
  $id=$this->uri->segment('3');
  $this->User->delete_user($id);
  //Redirecting User to Index Page after Delete Row
        redirect(base_url()."index.php/blog/");
 }
}
3. Model File: user.php
Codes for user.php (codeigniter/application/models)
<?php
class User extends CI_Model{
 //Loading Database in parent constructor
 function __construct(){
    parent::__construct();
         $this->load->database();
         } 
 public function Getuser(){
  //Select Data from user table
  $query= $this->db->get('user');
  return $query->result();
 }
 //Function to delete data by passing id
 public function delete_user($id){
  $this->db->where('id', $id);
        $this->db->delete('user');
 }
}
?>
Now run
http://localhost/codeigniter/index.php/blog
from Your browser and Enjoy.
Learn how to remove index.php from codeigniter
That's it codeigniter delete data from database.Keep Visiting for More Code..
Thank you..
codeigniter delete data from database
codeigniter delete data from database

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.