首页 > 解决方案 > 我不明白为什么会抛出“调用未定义的方法 CI_Input()::event()”

问题描述

我正在尝试使用表单将新记录(在这种情况下为事件)添加到数据库中。我不明白为什么我会收到以下错误:

这是我在控制器“Events.php”中的创建函数:


    public function create(){
        $data['title'] = 'Create Event';

        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('description', 'Description', 'required');
        $this->form_validation->set_rules('location', 'Location', 'required');
        $this->form_validation->set_rules('date', 'Date', 'required');
        $this->form_validation->set_rules('time', 'Time', 'required');

        if($this->form_validation->run() === FALSE){
            $this->load->view('templates/header');
            $this->load->view('events/create', $data);
            $this->load->view('templates/footer');
        } else {
            $this->Event_model->create_event();
            redirect('events');
        }

    }

然后这是我的“Event_model”中的 create_event 函数,用于尝试创建一个新的条目对象(?),然后将其添加到数据库中:


    public function create_event(){
            $slug = url_title($this->input->event('title'));

            $data = array(
                'title' => $this->input->event('title'),
                'description' => $this->input->event('description'),
                'date' => $this->input->event('date'),
                'time' => $this->input->event('time'),
                'location' => $this->input->event('location'),
                'slug' => $slug,
                'invitees' => $this->input->event('invitees')
            );

            return $this->db->insert('events', $data);
        }

我收到错误:

Message: Call to undefined method CI_Input::event()

从我第一次尝试create_event()分配$slug值时尝试在函数中调用“事件”。

我知道在我试图引用它之前,我没有在创建函数中将“事件”初始化为对象(或方法?),但我只是试图从表单中获取值并将它们传递给数据库,

在我遵循的指南中,他们没有收到此类错误

任何帮助将不胜感激

标签: phpcodeigniterphpmyadmin

解决方案


Input 类中没有event()方法。你应该使用or方法。post()get()

'title' => $this->input->post('title')

推荐阅读