【问题标题】:Codeigniter transferring data into model gives errorCodeigniter 将数据传输到模型中会出错
【发布时间】:2015-02-06 11:29:38
【问题描述】:

我想在 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')
            );
        // Transferring 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-&gt;db-&gt;insert('branches', $data);) 中。

我该怎么办?如何更改CI Core 文件?

注意:我找到了这个solution,但错误仍然存​​在。

请不要重复这篇文章,因为我尝试了多次搜索 Stackoverflow 并在我的代码中实现,但都失败了。

【问题讨论】:

  • construct 应该在 index 函数之外,而不是在里面。
  • 对不起,我打错了。我正在编辑我的帖子。

标签: php codeigniter codeigniter-2


【解决方案1】:

把你的 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');

    }
  }
}

【讨论】:

  • 是的,我已经更改了它并得到了另一个错误。请再次查看我的帖子。我已经编辑了我的帖子。
  • $autoload['libraries'] = array('database');检查是否已在 config.php 中设置
  • $autoload['libraries'] = array('database');这就是修复 "Message: Undefined property: Home::$db" 这个错误所需的全部内容。你确定你仍然得到同样的错误吗?
【解决方案2】:

@Raj,好的,你解决了这个问题。 好吧,这行不需要添加两次$autoload['libraries'] = array();

你应该只保留一行,即$autoload['libraries'] = array('database');

【讨论】:

  • 谢谢你。在我发布我的答案后我明白了。这些变量是连续的,我的意思是,一个接一个,我应该在它们所在的位置编写代码。
【解决方案3】:

在控制器中的 index() 函数之外设置 __construct()。

【讨论】:

  • 是的,我已经改过了,但是有错误(另一个错误)。
【解决方案4】:

最后一件事是我在$autoload['libraries'] = array(); 之前设置了$autoload['libraries'] = array('database');。因此它给出了错误。

感谢 karan thakkar,他发现了我的主要错误,即 constructor errorpritzzz for $autoload['libraries']

我的问题已经解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 2020-12-12
    • 2016-06-27
    • 2016-01-17
    • 1970-01-01
    相关资源
    最近更新 更多