简体   繁体   中英

How to Insert data to a table using codeigniter Active Record queries?

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


?>

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)

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;

    }

    } 
}    
?>

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM