2015-10-05 75 views
-1

模型視圖控制器項目上的代碼點火器 - 無法使用活動記錄類插入數據到表中。如何使用codeigniter Active Record查詢將數據插入表中?

錯誤

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Store::$Storemdl

Filename: controllers/Store.php

Line Number: 49

Backtrace:

File: C:\xampp\htdocs\ci\application\controllers\Store.php Line: 49 Function: _error_handler

File: C:\xampp\htdocs\ci\index.php Line: 292 Function: require_once

Fatal error: Call to a member function signupchk() on a non-object in C:\xampp\htdocs\ci\application\controllers\Store.php on line 49

A PHP Error was encountered

Severity: Error

Message: Call to a member function signupchk() on a non-object

Filename: controllers/Store.php

Line Number: 49

Backtrace:

模型

<?PHP 

defined('BASEPATH') OR exit('No direct script access allowed'); 

function __construct() 
{ 

    parent ::__construct(); 

    $this->load->database(); 
} 


function signupchk($username) 
{ 

    $this->db->select('username'); 

    $this->db->from('users'); 

    $this->db->where('username,$username'); 

    $sql = $this->db->get(); 


    if($sql && $sql->num_rows() > 0) 
    { 

     return false; 

    } 
    else 
    { 
     return true; 
    } 

} 



function signup($data) 
{ 


    $data = array(

     'rusername'=> 'username', 

     'rpassword'=> 'password', 

     'remail' => 'email', 

     'rphone' => 'phone', 

     'rgender' => 'gender', 

     'rqualify' => 'qualification', 

    ); 

    $sql = $this->db->insert('users',$data); 

    return $sql; 

} 

}  ?> 

控制器

<?php 


defined('BASEPATH') OR exit('No direct script access allowed'); 


class Store extends CI_Controller 


{ 


function index() 
{ 
    $this->load->view('home'); 
} 



function login() 
{ 
    $this->load->view('login'); 

} 

function signup() 
{ 
    $this->load->view('signup'); 

} 


function inserttbl(){ 


$username = $this->input->post('username'); 

$rslt = $this->Storemdl->signupchk($username); 

if($result){ 

    $this->Storemdl->signup($this->input->post()); 

    } 

    else 

    { 
     echo "username already exists"; 
    } 

    $this->index(); 


} 

} 


?> 
+0

爲什麼使用$ this-> Storemdl-> signupchk();你在配置文件 –

回答

0

張貼模型類的代碼和我n您的機型,在功能上signupchk($username)

+0

中爲模型使用適當的別名Active Record查詢是問題。相同的代碼工作沒有任何問題,內嵌查詢.. – Hisham

+0

,其中語法是錯誤的,取代我已經給了上面 –

0

哪裏是你的模型類改變這種

$this->db->where('username,$username'); 

$this->db->where('username',$username); 

or 

$this->db->where("username,$username");// single quote to double quote 

?你可以試試這個:

<?PHP 

    defined('BASEPATH') OR exit('No direct script access allowed'); 
    class Your_model_name extends CI_Model{ 
    function __construct() 
    { 

     parent ::__construct(); 

     $this->load->database(); 
    } 


    function signupchk($username) 
    { 

     $this->db->select('username'); 

     $this->db->from('users'); 

     $this->db->where('username,$username'); 

     $sql = $this->db->get(); 


     if($sql && $sql->num_rows() > 0) 
     { 

      return false; 

     } 
     else 
     { 
      return true; 
     } 

    } 



    function signup($data) 
    { 


     $data = array(

      'rusername'=> 'username', 

      'rpassword'=> 'password', 

      'remail' => 'email', 

      'rphone' => 'phone', 

      'rgender' => 'gender', 

      'rqualify' => 'qualification', 

     ); 

     $sql = $this->db->insert('users',$data); 

     return $sql; 

    } 

    } 
}  
?> 
0

你忘了加載你的模型。 在您的插入功能中添加以下行:

$this->load->model('Storemdl');

相關問題