在第37行的D:\ wamp \ www \ registration \ application \ controllers \ pages.php中调用未定义的方法CI_DB_mysqli_driver :: insert_item()

时间:2016-02-08 10:21:29

标签: php codeigniter codeigniter-3

我是CI的新手,我刚刚开始编码。我正在制作一个简单的联系表格并收到此错误。

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

这是我的controller.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);} }

3 个答案:

答案 0 :(得分:2)

您的控制器正在引用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);
    }
}

作为旁注 - 你能看到代码现在读起来有多容易吗?确保使用适当的缩进,因为它大大增加了代码的可读性。

答案 1 :(得分:1)

您已使用

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

但是insert_item是一个模型函数。

加载模型,然后通过以下模型调用:

$this->load->model('Blog');
$this->Blog->get_last_ten_entries();

答案 2 :(得分:0)

您需要确保正确调用model-&gt;函数。命名约定在2之间关闭(它们必须在它们出现在模型中时调用) data_submitted()中的最后一次调用应该是:$ this-&gt; user_model-&gt; insert_item($ data1);如下所示

控制器:

<?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);
}
}