【发布时间】:2014-05-16 05:42:06
【问题描述】:
我想使用 slug 在 codeigniter 中创建新闻项目,但错误显示是这样的。
> Call to a member function insert() on a non-object in line 34
我是 Codeigniter 的新手,无法真正弄清楚如何解决这个问题。
我的控制器
function tambah_profil()
{
$this->data['title'] ='create a new items';
$this->form_validation->set_rules('judul', 'Judul', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->data['contents'] = $this->load->view('admin/profil/tambah_profil', '', true);
}else{
$this->mhalaman->insert_profil();
$this->data['contents'] = $this->load->view('admin/profil/view_profil', '', true);
}
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['contents'] = $this->load->view('admin/profil/tambah_profil', '', true);
//$this->data['contents'] = 'admin/profil/tambah_profil';
$this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
}
我的模特
var $db;
private $tbl_halaman = 'halaman';
public function __construct()
{
parent ::__construct();
$this->load->database();
}
function get_profil($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get($this->tbl_halaman);
return $query->result_array();
}
$query = $this->db->get_where($this->tbl_halaman, array('slug'=>$slug));
return $query->row_array();
}
function insert_profil()
{
$slug = url_title($this->input->post('judul'),'dash', TRUE);
$data = array(
'judul' => $this->input->post('judul'),
'slug' => $slug,
'content' => $this->input->post('content')
);
return $this->db->insert($this->tbl_halaman ,$data); //line 34
}
请帮我做什么。谢谢。
【问题讨论】:
-
你加载数据库了吗?
-
是的,我有。我在这样的结构中加载数据库 $this->load->model(array('mlogin','mhalaman'));那该怎么办?
-
不必返回
$this->db->insert($this->tbl_halaman ,$data)。 ellislab.com/codeigniter/user-guide/database/active_record.html 供参考使用插入功能。 -
我已删除“返回”但同样的错误。怎么样?
标签: php codeigniter slug