Home >>Codeigniter Tutorial >Codeigniter delete database record
<?php class Hello extends CI_Controller { public function __construct() { parent::__construct(); $this->load->database(); $this->load->helper('url'); $this->load->model('Hello_Model'); } public function savedata() { $this->load->view('registration'); if($this->input->post('save')) { $n=$this->input->post('name'); $e=$this->input->post('email'); $m=$this->input->post('mobile'); $this->Hello_Model->saverecords($n,$e,$m); redirect("Hello/dispdata"); } } public function dispdata() { $result['data']=$this->Hello_Model->displayrecords(); $this->load->view('display_records',$result); } public function deletedata() { $id=$this->input->get('id'); $this->Hello_Model->deleterecords($id); redirect("Hello/dispdata"); } } ?>
<!DOCTYPE html> <html> <head> <title>Display Records</title> </head> <body> <table width="600" border="1" cellspacing="5" cellpadding="5"> <tr style="background:#CCC"> <th>Sr No</th> <th>Name</th> <th>Email</th> <th>Mobile</th> <th>Delete</th> </tr> <?php $i=1; foreach($data as $row) { echo "<tr>"; echo "<td>".$i."</td>"; echo "<td>".$row->name."</td>"; echo "<td>".$row->email."</td>"; echo "<td>".$row->mobile."</td>"; echo "<td><a href='deletedata?id=".$row->user_id."'>Delete</a></td>"; echo "</tr>"; $i++; } ?> </table> </body> </html>
<?php class Hello_Model extends CI_Model { function saverecords($name,$email,$mobile) { $query="insert into users values('','$name','$email','$mobile')"; $this->db->query($query); } function displayrecords() { $query=$this->db->query("select * from users"); return $query->result(); } function deleterecords($id) { $this->db->query("delete from users where user_id='".$id."'"); } } ?>