Poll: Is this post useful to you ??? You do not have permission to vote in this poll. |
|||
Yes | 1 | 33.33% | |
No | 2 | 66.67% | |
Total | 3 vote(s) | 100% |
* You voted for this item. | [Show Results] |
Edit data in a database table(CRUD part 03) |
This tutorial is about the editing the data a table.
Step 01. Frist create a database called School. And a table called Student_details in side the School database. Use the below mysql query---> create table Student_details(ID varchar(20) primary key,Name varchar(20),B_day varchar(20),Mobile varchar(20) ); And add some data in to the table using below query. INSERT INTO `student_details`(`ID`, `Name`, `B_day`, `Mobile`) VALUES ('001','Ujitha','1989 11 04','071-3546276') Step 02. Open database.php in Codeigniter/application/config directory. Do the following changes to it and save. $active_group = 'default'; $active_record = TRUE; $db['default']['hostname'] = 'localhost'; $db['default']['username'] = 'root'; //Enter your database username. $db['default']['password'] = ''; //Enter your database passowrd. $db['default']['database'] = 'school'; //Enter the database name. $db['default']['dbdriver'] = 'mysql'; $db['default']['dbprefix'] = ''; $db['default']['pconnect'] = TRUE; $db['default']['db_debug'] = TRUE; $db['default']['cache_on'] = FALSE; $db['default']['cachedir'] = ''; $db['default']['char_set'] = 'utf8'; $db['default']['dbcollat'] = 'utf8_general_ci'; $db['default']['swap_pre'] = ''; $db['default']['autoinit'] = TRUE; $db['default']['stricton'] = FALSE; Step 03. Open autoload.php and do the following modifications and save. $autoload['libraries'] = array('database', 'session', 'xmlrpc','email'); $autoload['helper'] = array('url','form'); Step 04. Open config.php do following midifications and save. $config['base_url'] = 'http://localhost/Codeigniter/'; $config['encryption_key'] = '23456789'; Step 05. Then do the bootstrp configarations as I told in previous tutorial.[This is not nessasary.] Now we have to create the MVC architecture. First create the Controller as editController.php <?php class editController extends CI_Controller { public function index() { $this->load->view('student'); } public function editData() { $this->load->library('form_validation'); $this->form_validation->set_rules('ID', 'ID', 'trim|required|min_length[2]|xss_clean'); $id= $this->input->post('ID'); if($this->form_validation->run()==FALSE) { $this->load->view('student'); echo '<script>alert("You have not fill the form correctly.Recheck");</script>'; } else { $this->load->model('editModel'); $data['info']=$this->editModel->retriveData($id); $this->load->view('editstudent',$data); } } public function editnewData(){ $data=array( 'ID' => $this->input->post('ID'), 'Name' => $this->input->post('Name'), 'B_day' => $this->input->post('B_day'), 'Mobile' => $this->input->post('Mobile') ); $this->load->model('editModel'); $this->editModel->editData($data); } } ?> Step 06 : Now create the model and save it as editModel.php <?php class editModel extends CI_Model{ public function retriveData($id) { $data = $this->db->get_where('student_details',array('ID'=>$id)); $this->db->delete('student_details',array('ID'=>$id)); return $data->result(); } public function editData($data) { $this->db->insert('student_details',$data); echo '<script>alert("You have update the data successfully.");</script>'; } } ?> Step 07 : Now create the one view as student.php <link href="<?php echo base_url('assets\css\bootstrap.css'); ?>" rel="stylesheet"> <link href="<?php echo base_url('assets\css\bootstrap.min.css'); ?>" rel="stylesheet"> <div class="container"> <?php echo form_open('editController/editData','class="form-inline"') ?> <div style="margin-left:30%;margin-top:10%;"> <div class="form-group"> <label>Enter ID : </label> <input type="text" class="form-control" name="ID"> </div> <button type="submit" class="btn btn-primary">Edit details</button> </div> <?php echo form_close() ?> </div> Step 08 : Now create another view as editstudent.php <link href="<?php echo base_url('assets\css\bootstrap.css'); ?>" rel="stylesheet"> <link href="<?php echo base_url('assets\css\bootstrap.min.css'); ?>" rel="stylesheet"> <div class="container"> <div style="margin-left:30%;margin-top:5%"> <?php echo form_open('editController/editnewData','class="form-horizontal"') ?> <?php foreach($info as $detail) ?> <div class="form-group"> <label for="ID" class="col-sm-2 control-label">ID :</label> <div class="col-sm-3"> <input type="text" class="form-control" id="ID" name="ID" placeholder="ID" value="<?php echo $detail->ID ?>"> </div> </div> <div class="form-group"> <label for="name" class="col-sm-2 control-label">Name :</label> <div class="col-sm-3"> <input type="text" class="form-control" id="Name" placeholder="Name" name="Name" value="<?php echo $detail->Name ?>"> </div> </div> <div class="form-group"> <label for="inputEmail3" class="col-sm-2 control-label">Birth day :</label> <div class="col-sm-3"> <input type="text" class="form-control" id="B_day" name="B_day" placeholder="Birth day" value="<?php echo $detail->B_day ?>"> </div> </div> <div class="form-group"> <label for="inputEmail3" class="col-sm-2 control-label">Mobile :</label> <div class="col-sm-3"> <input type="text" class="form-control" id="Mobile" name="Mobile" placeholder="Birth day" value="<?php echo $detail->Mobile ?>"> </div> </div> </br> <div > <div style="margin-left:20%"> <input type="submit" value="Update" class="btn btn-primary" /> </div> </div> <?php echo form_close() ?> </div> </div> Step 08 : That is all.Go to webbrowser and type below in addressbar. http://localhost/Codeigniter/index.php/editController Update the data as you want.
That is my style with Codeigniter . Start with simple and ends in deep.
PLEASE use MyCode (http://forum.codeigniter.com/misc.php?action=help&hid=7) in your posts, to control the size of code and prevent your posts from running on.
|