我想在Codeigniter中保存表单中的数据。
控制器:home.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('insert_model');
}
public function index(){
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('custName', 'Customer Name', 'required|min_length[1]|max_length[50]');
$this->form_validation->set_rules('custPhone', 'Contact No.', 'required|min_length[1]|max_length[50]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('home');
} else {
$data = array(
'custName' => $this->input->post('custName'),
'custPhone' => $this->input->post('custPhone')
);
// Transfering Data To Model
$this->insert_model->insertData($data);
$this->load->view('home');
}
}
}
型号:insert_model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Insert_model extends CI_Model{
function __construct(){
parent::__construct();
}
function insertData($data){
$this->db->insert('branches', $data);
}
}
查看:home.php
<div id="container">
<?php echo form_open('home'); ?>
<h1>Save Data</h1>
<?php echo form_label('Customer Name :'); ?> <?php echo form_error('custName'); ?>
<?php echo form_input(array('id' => 'custName', 'name' => 'custName')); ?><br />
<?php echo form_label('Contact No. :'); ?> <?php echo form_error('custPhone'); ?>
<?php echo form_input(array('id' => 'custPhone', 'name' => 'custPhone')); ?><br />
<?php echo form_submit(array('id' => 'submit', 'value' => 'Save'));?>
<?php echo form_close(); ?>
</div>
当我运行它时,它会出错
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Home::$db
Filename: core/Model.php
Line Number: 52
WAMP服务器提供Fatal error: Call to a member function insert() on a non-object in C:\wamp\www\code\admin\application\models\insert_model.php on line 11
此错误发生在我的insert_model ($this->db->insert('branches', $data);)
。
我该怎么办?如何更改CI Core
个文件?
注意:我发现了solution,但错误仍然存在。
请不要将此帖重复,因为我多次尝试搜索Stackoverflow
并在我的代码中实现,但失败了。
答案 0 :(得分:2)
将home.php控制器更改为此。
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('insert_model');
}
public function index(){
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('custName', 'Customer Name', 'required|min_length[1]|max_length[50]');
$this->form_validation->set_rules('custPhone', 'Contact No.', 'required|min_length[1]|max_length[50]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('home');
} else {
$data = array(
'custName' => $this->input->post('custName'),
'custPhone' => $this->input->post('custPhone')
);
// Transfering Data To Model
$this->insert_model->insertData($data);
$this->load->view('home');
}
}
}
答案 1 :(得分:1)
@Raj,好的,你修好了。
好吧,不需要将此行添加两次$autoload['libraries'] = array();
您应该只保留一行,即$autoload['libraries'] = array('database');
答案 2 :(得分:0)
在控制器中的index()函数之外设置__construct()。
答案 3 :(得分:0)
最后一件事是我在$autoload['libraries'] = array('database');
之前设置了$autoload['libraries'] = array();
。因此它给出了错误。
感谢 karan thakkar ,我发现constructor error
$autoload['libraries']
和 pritzzz
我的问题已经解决了。