Update data using codeigniter

code of edit.php file located at application/views :-

edit.php

<html> <head> </head> <body> <form method="post" action=<?php echo base_url()?>Cnt/update_data> <table> <?php foreach($data as $v) { ?> <tr> <td>Name</td> <td><input type="text" name="emp_name" id="emp_name" value=<?php echo $v->emp_name?>></td> </tr> <tr> <td>Salary</td> <td><input type="number" name="emp_salary" id="emp_salary" value=<?php echo $v->emp_salary?>></td> </tr> <input type="hidden" name="hdn" value="<?php echo $v->emp_id?>"> <tr> <td><input type="submit" name="btn" value="Update"></td> </tr> </table> <?php } ?> </form> </body></html>

Controller code for edit and update :-


<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Cnt extends CI_Controller {
  
      public function edit_data(){
$ed_id=$this->input->get('ed');

$resp=$this->Model->edit_data('emp1',$ed_id);
$this->load->view('edit',['data'=>$resp]);
}
public function update_data(){
$edit_id=$this->input->post('hdn');
$name=$this->input->post('emp_name');
$salary=$this->input->post('emp_salary');
$arr=array(
'emp_name'=>$name,
'emp_salary'=>$salary
);
$resp=$this->Model->update_data('emp1',$arr,$edit_id);
echo "<script>alert('$resp')</script>";
$this->display_view();
}
}

Model code for edit and update:-


<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Model extends CI_Model {

        public function edit_data($tbl,$ed_id)
{
$this->db->from($tbl);
$this->db->where('emp_id',$ed_id);
$query = $this->db->get();
return $query->row();
}
public function update_data($tbl,$arr,$ed_id){
$this->db->where('emp_id',$ed_id);
$this->db->update($tbl,$arr);
return "Record update Successfully";
}
}


Comments

Popular Posts