我在使用多个数据库时在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");
}
}
配置(database.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)
但仍会出现同样的错误
我错过了什么吗?感谢