Tuesday 10 March 2015

How to insert value FORM to DATABASE using CodeIgniter?

(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   'form.php' save it into views folder of codeigniter:


<form action="<?php echo site_url('site/insert'); ?>"  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  'site_model.php'  save it into models folder of codeigniter:

<?php

class site_model extends CI_Model

{


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

public function insertdata($data)

{

$this->load->database();



$this->db->insert('tbl_name', $data);

}

}

?>

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


<?php

class site extends CI_Controller

{

function __construct()

{

parent::__construct();

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

}
function index()

{

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

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




}

function insert()

{




$this->load->database();

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




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


}

}


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

(where ci  is  codeigniter )








No comments:

Post a Comment