我是CI的新手,我刚刚开始编码。我正在制作一个简单的联系表格,但得到了这个错误。
Call to undefined method CI_DB_mysqli_driver::insert_item() in
D:'wamp'www'registration'application'controllers'pages.php on line 37
这是我的控制器的代码.php
<?php class Pages extends CI_Controller { public function view($page = 'home'){
if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the 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); } } ?>
这是我模型的完整代码
<?php class User_model extends CI_Model { private $item; function __construct(){
/* Call the Model constructor */
parent::__construct(); }
function insert_item($item){
/*$this->table = "contact";
$this->item = $item;
*/
//$dbconnect = $this->load->database();
$this->db->insert("contact", $item);} }
控制器引用db
库:
$this->db->insert_item($data1);
但是,insert_item()
方法仅存在于您的模型中!
它应该看起来像这样:
$this->load->model('my_model');
$this->my_model->insert_item($data1);
在此处编辑以考虑您的控制器代码。
假设user_model
已加载,并且user_model
包含要更改的方法insert_item()
$this->user_model->insert($data1);
自
$this->user_model->insert_item($data1);
看过完整的控制器和型号代码...
您的控制器:
您正在view()
方法中加载用户模型,但从我所看到的情况来看,您实际上并没有使用它。但是,您在data_submitted()
中使用了该模型,但它从未加载到那里。您还使用了insert
而不是insert_item()
。下面是控制器的更正代码。
public function view($page = 'home') {
if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the 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);
}
您的型号:
似乎有一些代码完全没有做任何事情。我已经将其删除,以下代码应该可以工作。
class User_model extends CI_Model {
function __construct(){
/* Call the Model constructor */
parent::__construct();
}
public function insert_item($item){
$this->db->insert("contact", $item);
}
}
作为旁注 - 您能看到代码现在阅读起来有多容易吗?确保使用正确的缩进,因为它可以大大提高代码的可读性。
您已使用
$this->db->insert_item($data1);
但insert_item
是一个模型函数。
然后通过模型调用,如下所示:
$this->load->model('Blog');
$this->Blog->get_last_ten_entries();
您需要
确保正确调用模型>函数。命名约定在 2 之间关闭(它们必须在模型中出现时调用(data_submitted(( 中的最后一次调用应为:$this->user_model->insert_item($data 1(;如下
控制器:
<?php class Pages extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('user_model');
}
public function view($page = 'home')
{
if (!file_exists(APPPATH . '/views/pages/' . $page . '.php')) {
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the 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->db->set($data1);
$this->user_model->insert_item($data1);
}
} ?>
型:
<?php class User_model extends CI_Model
{
function __construct()
{
/* Call the Model constructor */
parent::__construct();
$this->load->database();
}
private $item;
function insert_item($item)
{
/*$this->table = "contact";
$this->item = $item;
*/
//$dbconnect = $this->load->database();
$this->db->insert("contact", $item);
}
}