2012-03-04 136 views
0

我得到一個錯誤,當我運行這個簡單的插入操作請看看調用一個成員函數add()一個非對象

ERROR:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Welcome::$mdata
Filename: controllers/welcome.php
Line Number: 29

Fatal error: Call to a member function add() on a non-object in C:\wamp\www\codi\application\controllers\welcome.php on line 29

我使用笨

創建基於MVC代碼

我的型號名稱爲mdata.php 爲:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
class Mdata extends CI_Model { 
function Mdata(){ 
parent::__construct(); 
} 
function add($id, $name) { 
     $data = array(
      'userid' => $id, 
      'username' => $name 
     ); 

     $this->db->insert('users', $data); 
    } 
} 
?> 

我的控制器命名爲歡迎。 PHP是

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
class Welcome extends CI_Controller 
{ 

public function index() 

{ 
     if($this->input->post('submit')) 
     { 
      $id = $this->input->post('id'); 
      $name = $this->input->post('name'); 
      $this->mdata->add($id,$name); 
      } 

$this->load->model('mdata','',TRUE); 

$this->load->view('data'); 

} 

} 

======================================== ===================================== 我的觀點是data.php文件爲

<body> 
<?php $this->load->helper('form'); ?> 

<?php echo form_open('welcome'); ?> 

    <p> 
     <?php echo form_input('id'); ?> 
    </p> 

    <p> 
     <?php echo form_input('name'); ?> 
    </p> 

    <p> 
     <?php echo form_submit('submit', 'Submit'); ?> 
    </p> 

<?php echo form_close(); ?> 
</body> 

任何幫助我解決這個簡單的錯誤?

回答

3

您需要之前,加載由$this->mdata引用的模型調用add()

if($this->input->post('submit')) 
{ 
    // load the model first! 
    $this->load->model('mdata'); 

    $id = $this->input->post('id'); 
    $name = $this->input->post('name'); 

    $this->mdata->add($id,$name); 
} 
相關問題