【问题标题】:Fatal error: Call to a member function, on a non-object, codeigniter controller致命错误:在非对象、codeigniter 控制器上调用成员函数
【发布时间】:2014-10-08 12:48:15
【问题描述】:

这是我的控制器 profileinfo.php

<?php
class Profileinfo extends CI_Controller {

    function index()
    {

        $this->load->helper(array('form', 'url'));
        $this->load->model('aluminsert','', TRUE);
        $this->load->library('form_validation');

        $this->form_validation->set_rules('name', 'Name', 'required');
        $this->form_validation->set_rules('branch', 'Bramch', 'required');
        $this->form_validation->set_rules('academicinterest', 'Academic Interest', 'required');
        $this->form_validation->set_rules('internshipdetail', 'Internship Details', 'required');
        $this->form_validation->set_rules('interest_hobbies', 'Interest and hobbies', 'required');
        $this->form_validation->set_rules('personalblog', 'Personal Blog', 'required');
        $this->form_validation->set_rules('facebookprofile', 'Facebook Profile', 'required');
        $this->form_validation->set_rules('linkedinprofile', 'Linkedin Profile', 'required');
        $this->form_validation->set_rules('homecity', 'Home city', 'required');
        $this->form_validation->set_rules('country', 'country', 'required');
        $this->form_validation->set_rules('higherdegree', 'Higher Degree', 'required');
        $this->form_validation->set_rules('collegeofeducation', 'College of Education', 'required');
        $this->form_validation->set_rules('officialemail', 'Official Email', 'required');
        $this->form_validation->set_rules('careerinterests', 'Career Interest', 'required');
        $this->form_validation->set_rules('presentemployer', 'Present Employer', 'required');
        $this->form_validation->set_rules('presentjob', 'Present Job', 'required');
        $this->form_validation->set_rules('previousemployer', 'Previous Employee', 'required');
        $this->form_validation->set_rules('startupname', 'Startup Name', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('alumprofile');

        }
        else
        {
            $alumdata = array(
            'Name' => $this->input->post('name'),
            'Branch' => $this->input->post('branch'),
            'Academic Interest' => $this->input->post('academicinterest'),
            'Internship Details' => $this->input->post('internshipdetail'),
            'Interest and hobbies' => $this->input->post('interest_hobbies'),
            'personalblog' => $this->input->post('personalblog'),
            'Facebook profile' => $this->input->post('facebookprofile'),
            'Linkedin profile' => $this->input->post('linkedinprofile'),
            'Home city' => $this->input->post('homecity'),
            'country' => $this->input->post('country'),
            'Higher Degree' => $this->input->post('higherdegree'),
            'College of Education' => $this->input->post('collegeofeducation'),
            'Official Email' => $this->input->post('officialmail'),
            'Career Interest' => $this->input->post('careerinterests'),
            'Present Employer' => $this->input->post('presentemployer'),
            'Present Job' => $this->input->post('presentjob'),
            'Previous Employee' => $this->input->post('previousemployer'),
            'Startup Name' => $this->input->post('startupname'),
            );
            // Transfering Data To Model
            $this->insert->alum_insert($alumdata);
            $this->load->view('registration_success');
        }
    }
}
?>

这是我的模型 aluminsert.php

<?php
class Aluminsert extends CI_Model{
    function __construct() {
        parent::__construct();
    }
    function alum_insert($alumdata){

        $this->db->insert('user_detail', $alumdata);
    }
}
?>

表单验证正在工作,提交表单后,显示的错误是: 致命错误:在第 61 行的 C:\wamp\www\ci\application\controllers\profileinfo.php 中的非对象上调用成员函数 alum_insert()

【问题讨论】:

  • 型号名称是aluminsert 不是insert 它应该是$this-&gt;alum_insert-&gt;alum_insert($alumdata);
  • @karanthakkar 差不多了。 $this-&gt;aluminsert-&gt;alum_insert($alumdata)
  • @MarioCesar 是的,我的错字错了;)

标签: php database codeigniter codeigniter-2


【解决方案1】:

错误在于下面的代码。您正在尝试从“插入”类中调用一个方法,在这种情况下,您的模型中不存在该方法。

$this->insert->alum_insert($alumdata);
$this->load->view('registration_success');

您的模型称为“Aluminsert”,您用于插入的方法称为“alum_insert”,因此您应该这样做。

$this->aluminsert->alum_insert($alumdata);
$this->load->view('registration_success');

【讨论】:

    【解决方案2】:

    在 codeigniter 中,我们需要加载数据库。所以有两种方法

    1. 您可以自动加载它或
    2. 在需要的地方加载。

    autoload.php中,你必须写下下面一行。

    $autoload['libraries'] = array('database');

    您也可以通过以下方式加载其他方式:-

    $this-&gt;load-&gt;database();

    肯定会对你有所帮助。

    【讨论】:

    • 这与问题有什么关系?
    猜你喜欢
    • 2014-10-17
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    相关资源
    最近更新 更多