Tuesday 10 March 2015

How to Update value from a form to database in codeigniter Framework?



(1)create table  'tbl_name':

create table tbl_name

(id int(5) primary key auto_increment,

f1   varchar(200),

f2  varchar(200),

f3  varchar(200));


(2) write code for   'upform.php' save it into views folder of codeigniter:


<form action="<?php echo site_url('up/upd'); ?>"  method=post>

Field 1 <input type = ‘text’ name='f1'><br>

Field 2 <input type = ‘text’ name='f2'><br>

Field 3 <input type = ‘text’ name='f3'><br>

<input type="submit"  name="submit"  value="submit">

</form>


(3)write code for  'update_model.php'  save it into models folder of codeigniter:

<?php

class update_model extends CI_Model

{


function __construct()
        {
            parent::__construct();
        }

       
public function update($data) {
  $f1 = $data['f1'];
  unset($data['f1']);
  $this->db->where('f1', $f1);
  $this->db->update('tbl_name' ,$data);
  return true;
}

}

?>

(4)write code for 'up.php'  save it into  models folder of   controllers:


<?php

class up extends CI_Controller

{

function __construct()

{

parent::__construct();

$db = $this->load->database();

}
function index()

{

$this->load->helper('url');

$this->load->view('upform');




}

function upd()

{




$this->load->database();

$this->load->model('update_model');




                      
                      
                      
                      
                       $data = array(
          
           'f1' => $this->input->post('f1'),
           'f2' => $this->input->post('f2'),
           'f3' => $this->input->post('f3')
          
        );
                      
$this->update_model->update($data);



}

}

(5)Then  opend browser   &  type    'http://localhost/ci/index.php/up'


(where ci  is   codeigniter )








No comments:

Post a Comment