How to Delete data from database in MySQL and Codeigniter

Hello All,

I would like to share about how to delete data from database using Mysql Codeigniter.
All of you can download by click here

1- Create DB
 
--
-- Table structure for table `data`
--

CREATE TABLE IF NOT EXISTS `data` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `description` varchar(250) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;

--
-- Dumping data for table `data`
--

INSERT INTO `data` (`id`, `description`) VALUES
(1, 'test1'),
(2, 'test2'),
(3, 'test3'),
(4, 'test4'),
(5, 'test5'),
(6, 'test6'),
(7, 'test7'),
(8, 'test8'),
(10, 'test10');

2- Create Controller(welcome.php)
load->model('main_model');
	}
	public function index()
	{
		// select data from model
		$data['data'] = $this->main_model->getAlldata();
		$this->load->view('welcome_message',$data);
	}
	// delete data function
	function deleteData($id_data){
		$result = $this->main_model->deleteDataId($id_data);
		if($result){
			$this->session->set_flashdata('success', 'Delete success');
			redirect('welcome');
		}else{
			$this->session->set_flashdata('unsuccess', 'Delete fail!');
			redirect('welcome');
		}
	}
}


3- Create Model(main.php)
db->get('data');
		if($query->num_rows() > 0){
			return $query->result();
		}
		return array();
	}
	function deleteDataId($id_data){
		$this->db->where('id', $id_data);
		$query = $this->db->delete('data');
		return $query;
	}
}

4- Create View(welcome_message.php)


	
	Web develp sharing
	
	


	
session->flashdata("success")): ?>
session->flashdata("unsuccess")): ?>
IDDescriptionAction
id;?> description;?>

Note
Don't forget config database and load librarry
Thanks
Share on Google Plus

About Unknown

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment