I would like to share about how to Update, 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'); } } } //form update function formupdate($id){ //get data via id $data['data'] = $this->main_model->getDataId($id); $this->load->view('formupdate',$data); } function update(){ if($this->input->post('btn_update')){ $id = $this->input->post('id'); $data = array( 'description' => $this->input->post('data'), ); $result = $this->main_model->update($id, $data); if($result){ $this->session->set_flashdata('success', ' success'); redirect('welcome'); }else{ $this->session->set_flashdata('unsuccess', ' 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; } }
function getDataId($id){ $this->db->where('id', $id); $query = $this->db->get('data'); if($query->num_rows() > 0){ return $query->result(); } return array(); } function update($id, $data){ $this->db->where('id', $id); $query = $this->db->update('data', $data); return $query; }
4- Create View(welcome_message.php)
Web develp sharing
form update (formupdate.php)
Web develp sharing id.'" />
'; echo '
'; echo ''; }?>
Note
Don't forget config database and load librarry
Thanks
0 comments:
Post a Comment