codeigniter - Call to undefined method CI_DB_mysqli_driver::insert_item() in D:\wamp\www\registration\application\controllers\pages.php on line 37 -


i new ci , have started code. making simple contact form , got error.

call undefined method ci_db_mysqli_driver::insert_item() in  d:\wamp\www\registration\application\controllers\pages.php on line 37 

here code of controller.php

<?php class pages extends ci_controller { public function view($page = 'home'){  if ( ! file_exists(apppath.'/views/pages/'.$page.'.php')) {     // whoops, don't have page that!     show_404(); }  $data['title'] = ucfirst($page); // capitalize first letter  $this->load->model('user_model');  //$this->load->view('templates/header', $data); $this->load->view('pages/'.$page, $data); //$this->load->view('templates/footer', $data);} public function data_submitted(){ $name = $this->input->get('name'); $email = $this->input->get('email'); $mobile = $this->input->get('mobile'); $address = $this->input->get('address'); $gender = $this->input->get('gender');  $data1 = array(     'name'=> $name,     'email'=>$email,     'mobile'=>$mobile,     'address'=>$address,     'gender'=>$gender ); //$this->db->set($data1); $this->user_model->insert($data1); } } ?> 

and here full code of model

<?php class user_model extends ci_model { private $item; function __construct(){ /* call model constructor */ parent::__construct(); } function insert_item($item){ /*$this->table = "contact"; $this->item = $item; */ //$dbconnect = $this->load->database(); $this->db->insert("contact", $item);} } 

your controller referencing db library:

$this->db->insert_item($data1); 

however, insert_item() method exists in model!

it should this:

    $this->load->model('my_model');     $this->my_model->insert_item($data1); 

edit here take account controller code.

assuming user_model has been loaded, , user_model contains method insert_item() want change

$this->user_model->insert($data1); 

to

$this->user_model->insert_item($data1); 

having seen complete controller , model code...

your controller:

you loading user model in view() method, can see you're not using it. using model in data_submitted() never loaded there. using insert rather insert_item(). here corrected code controller.

public function view($page = 'home') {     if ( ! file_exists(apppath.'/views/pages/'.$page.'.php'))     {         // whoops, don't have page that!         show_404();     }      $data['title'] = ucfirst($page); // capitalize first letter      //$this->load->view('templates/header', $data);     $this->load->view('pages/'.$page, $data);     //$this->load->view('templates/footer', $data); }  public function data_submitted(){     $name = $this->input->get('name');     $email = $this->input->get('email');     $mobile = $this->input->get('mobile');     $address = $this->input->get('address');     $gender = $this->input->get('gender');      $data1 = array(         'name'=> $name,         'email'=>$email,         'mobile'=>$mobile,         'address'=>$address,         'gender'=>$gender     );      $this->load->model('user_model');     $this->user_model->insert_item($data1); } 

your model:

there seemed code did absolutely nothing. have stripped out , following code should work.

class user_model extends ci_model {     function __construct(){         /* call model constructor */         parent::__construct();     }       public function insert_item($item){         $this->db->insert("contact", $item);     } } 

as side note - can see how easy code read now? make sure use proper indentation massively increases readability of code.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -