duangua6912
2015-10-05 06:45
浏览 33
已采纳

如何使用codeigniter Active Record查询将数据插入表中?

MODEL VIEW CONTROLLER PROJECT ON CODE IGNITER - UNABLE TO INSERT DATA TO THE TABLE USING Active Record Class .

ERROR

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:

MODEL

<?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;

}

}     ?>

CONTROLLER

<?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();


}

}


?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dotymq4217 2015-10-05 06:55
    已采纳

    post the code for model class and, in your model, change this

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

    to

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

    in function signupchk($username)

    点赞 打赏 评论
  • duanbi3385 2015-10-05 06:55

    Where is your model class? You can try this:

     <?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;
    
        }
    
        } 
    }    
    ?>
    
    点赞 打赏 评论
  • dongtaigan1594 2015-10-05 07:29

    You forget to load your model. Add this line in your insert function:

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

    点赞 打赏 评论

相关推荐 更多相似问题