Welcome Guest, Not a member yet? Register   Sign In
Fatal error: Call to a member function insert() on a non-object in ?
#1

[eluser]anna16[/eluser]
Guys

I have encountered error in CI 2.0
Code:
Fatal error: Call to a member function insert() on a non-object in /home/coder9/public_html/ci2/application/models/membership/membership.php on line 7

and the here is the codes of membership.php
Code:
<?php
class Membership extends CI_Model
{

    function add_record($data)
    {
        $this->db->insert('user', $data);
        return;
    }

}
//end of membership_model

thanks in advanced.
#2

[eluser]InsiteFX[/eluser]
Hi anna,

Did you load the database?

You can autoload it in application/config/autoload.php

InsiteFX
#3

[eluser]anna16[/eluser]
@InsiteFX

is this the right command below for autoloading the database?

Code:
$autoload['database'] = array('auto connect');

Please correct me if I'm wrong.

Thanks.
#4

[eluser]tonanbarbarian[/eluser]
you have not constructed the class properly

Code:
class Membership extends Model
{

    function Membership()
    {
        parent::Model();
    }

    function add_record($data)
    {
        $this->db->insert('user', $data);
        return;
    }

}
//end of membership_model
#5

[eluser]anna16[/eluser]
thanks tonanbarbarian

I already did what you said.

but i have new error,
Code:
Fatal error: Class 'CI_Controller' not found in /home/coder9/public_html/ci2/application/controllers/membership/site.php on line 2
#6

[eluser]Learn CodeIgniter[/eluser]
CI 2.0
application/config/autoload.php

Code:
$autoload['libraries'] = array('database', 'session');
#7

[eluser]Learn CodeIgniter[/eluser]
application/config/config.php
Code:
/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
|    http://example.com/
|
*/
$config['base_url']    = BASE_URL;

/*
|--------------------------------------------------------------------------
| Cookie Related Variables
|--------------------------------------------------------------------------
|
| 'cookie_prefix' = Set a prefix if you need to avoid collisions
| 'cookie_domain' = Set to .your-domain.com for site-wide cookies
| 'cookie_path'   =  Typically will be a forward slash
|
*/
$config['cookie_prefix'] = "";
$config['cookie_domain'] = ($_SERVER['SERVER_NAME'] == 'localhost' ? '' : preg_replace('/^www\./', '', $_SERVER['SERVER_NAME']));
$config['cookie_path'] = BASE_URI;

Add this to the bottom of:

application/config/constants.php
Code:
/*
|--------------------------------------------------------------------------
| Docment root folders
|--------------------------------------------------------------------------
|
| These constants use existing location information to work out web root, etc.
|
*/

// Base URL (keeps this crazy sh*t out of the config.php
if (isset($_SERVER['HTTP_HOST']))
{
    $base_url  = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
    $base_url .= '://'. $_SERVER['HTTP_HOST'];
    $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);

    // Base URI (It's different to base URL!)
    $base_uri = parse_url($base_url, PHP_URL_PATH);

    if (substr($base_uri, 0, 1) != '/')
    {
        $base_uri = '/'.$base_uri;
    }

    if (substr($base_uri, -1, 1) != '/')
    {
        $base_uri .= '/';
    }
}

else
{
    $base_url = 'http://localhost/';
    $base_uri = '/';
}

// Define these values to be used later on
define('BASE_URL', $base_url);
define('BASE_URI', $base_uri);
define('APPPATH_URI', BASE_URI.APPPATH);

// We dont need these variables any more
unset($base_uri, $base_url);

Thanks to the PyroCMS Team for this code!
#8

[eluser]anna16[/eluser]
@learn codeigniter

I already did what you said, but the problem still the same.
#9

[eluser]anna16[/eluser]
okay I'll try your last post.
#10

[eluser]Learn CodeIgniter[/eluser]
With that code you will never need to set a base_url again.




Theme © iAndrew 2016 - Forum software by © MyBB