Welcome Guest, Not a member yet? Register   Sign In
unable to update record
#14

(This post was last modified: 07-31-2016, 09:48 AM by Wouter60.)

There are probably more roads to the same solution, but I'll try to describe the approach that is most common in CI.
This is a very basic example, using model, view and controller.

Controller: Stud_controller
PHP Code:
public function update_student($id)
{
   
$this->load->model('stud_model');
 
  if ( ! $this->input->post('submit') ) {
 
       // if the form is not yet submitted, then show the form
 
       $data['record'] = $this->stud_model->get_student($id);
 
       $this->load->view('student_form',$data);
 
  }
 
  else {
 
       // form is submitted; update the record in the database
 
       $data = array(
 
           'name' => $this->input->post('name'),
 
           'roll_no' => $this->input->post('roll_no')
 
       );
 
       $this->stud_model->update_student($id,$data);
 
       redirect('/');  //return to your home page
 
  }


Model: stud_model
PHP Code:
public function get_student($id)
{
//fetch a record from the table 'students', for the given id
$query $this->db->get_where('students''id=' $id1);
if (
$query->num_rows() > 0) {
 
  return $this->query->row_array();
}
else {
 
  return FALSE;
}
}

public function 
update_student($id,$data)
{
//update the record in the table 'students'
$this->db->update('students'$data'id=' $id);


View student_form
PHP Code:
<?php 
if (! $record) {
 
   show_error('There is no student with this id in the database');
 
   die();
}
echo 
form_open();  //the form will be submitted to the same url as it was called from
echo 'Name: ' form_input('name',$record['name']) . '<br />';
echo 
'Roll_no: ' form_input('roll_no'$record['roll_no']) . '<br />';
echo 
form_submit('submit','Submit form');  //important: the submit button should have a name, in this case: 'submit'
echo form_close();
?>
Reply


Messages In This Thread
unable to update record - by nadeem14375 - 07-27-2016, 11:16 AM
RE: unable to update record - by mwhitney - 07-27-2016, 11:57 AM
RE: unable to update record - by nadeem14375 - 07-27-2016, 12:18 PM
RE: unable to update record - by nadeem14375 - 07-27-2016, 12:20 PM
RE: unable to update record - by mwhitney - 07-28-2016, 10:35 AM
RE: unable to update record - by Wouter60 - 07-28-2016, 08:46 AM
RE: unable to update record - by jaynarayan - 07-30-2016, 10:36 AM
RE: unable to update record - by nadeem14375 - 07-30-2016, 11:14 AM
RE: unable to update record - by InsiteFX - 07-30-2016, 11:37 AM
RE: unable to update record - by nadeem14375 - 07-30-2016, 11:52 AM
RE: unable to update record - by nadeem14375 - 07-30-2016, 12:37 PM
RE: unable to update record - by Wouter60 - 07-30-2016, 11:08 PM
RE: unable to update record - by nadeem14375 - 07-31-2016, 02:18 AM
RE: unable to update record - by Wouter60 - 07-31-2016, 05:50 AM



Theme © iAndrew 2016 - Forum software by © MyBB