0

当您使用多个数据库时,我无法在 codeigniter 中提交表单

出现的错误是......

未定义的属性

致命错误:在非对象上调用成员函数 insert()

这是我的代码

控制器(site.php)

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Site extends CI_controller {

        public function __construct()
        {
            parent::__construct();
            // Your own constructor code
            $this->load->model("site_model");

            //$cms_db = $this->load->database('cms', TRUE); /* database conection for write operation */
            $site_db = $this->load->database('default', TRUE);  
        } 

        public function index()
        {
        $this->load->helper('form');

        $data = $this->_initialize_data();
        $this->load->vars($data);
        $this->load->view('site/index');
        }

        public function learner()
        {
            $cms_db = $this->load->database('cms', TRUE); /* database conection for write operation */
            if(isset($_POST['submit_learner']))
                {
                    $fname = $this->input->post("fname");
                    $mname = $this->input->post("mname");
                    $lname = $this->input->post("lname");
                    $ename = $this->input->post("ename");
                    $bday = $this->input->post("bday");
                    $opt = $this->input->post("opt");
                    $educ = $this->input->post("educ");
                    $als = $this->input->post("als");
                    $brgy = $this->input->post("brgy");
                    $guardian = $this->input->post("guardian");
                    $relation = $this->input->post("relation");
                    $benefit = $this->input->post("benefit");

                    $newdata = array('first_name'=>$fname,
                                        'middle_name'=>$mname,
                                        'last_name'=>$lname,
                                        'extention_name'=>$ename,
                                        'bday'=>$bday,
                                        'gender'=>$opt,
                                        'education'=>$educ,
                                        'als'=>$als,
                                        'brgy'=>$brgy,
                                        'guardian'=>$guardian,
                                        'relationship'=>$relation,
                                        'added_by'=>'OTH',
                                        'added_id'=>''
                                        );

                    $this->cms_db->insert('records', $newdata); 

                    redirect(base_url() . "site/index");
                }
        }

配置(数据库.php)

    $active_group = 'default';
    $active_record = TRUE;

    $db['default']['hostname'] = 'localhost';
    $db['default']['username'] = 'root';
    $db['default']['password'] = '';
    $db['default']['database'] = 'dbsite';
    $db['default']['dbdriver'] = 'mysql';
    $db['default']['dbprefix'] = '';
    $db['default']['pconnect'] = TRUE;
    $db['default']['db_debug'] = TRUE;
    $db['default']['cache_on'] = FALSE;
    $db['default']['cachedir'] = '';
    $db['default']['char_set'] = 'utf8';
    $db['default']['dbcollat'] = 'utf8_general_ci';
    $db['default']['swap_pre'] = '';
    $db['default']['autoinit'] = TRUE;
    $db['default']['stricton'] = FALSE;

    $db['cms']['hostname'] = 'localhost';
    $db['cms']['username'] = 'root';
    $db['cms']['password'] = '';
    $db['cms']['database'] = 'dbcms';
    $db['cms']['dbdriver'] = 'mysql';
    $db['cms']['dbprefix'] = '';
    $db['cms']['pconnect'] = FALSE;
    $db['cms']['db_debug'] = TRUE;
    $db['cms']['cache_on'] = FALSE;
    $db['cms']['cachedir'] = '';
    $db['cms']['char_set'] = 'utf8';
    $db['cms']['dbcollat'] = 'utf8_general_ci';
    $db['cms']['swap_pre'] = '';
    $db['cms']['autoinit'] = TRUE;
    $db['cms']['stricton'] = FALSE;

似乎由于我的控制器(site.php)而发生错误

$this->cms_db->insert('records', $newdata); 

它说 cms_db 是一个未定义的属性

我也试过这样

$cms_db->insert('records',$newdata)

但仍然出现同样的错误

我错过了什么吗?

谢谢

4

0 回答 0