简体   繁体   中英

Undefined property on codeigniter controller

I'm trying to insert some form data with a model

this is the controller

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

class Welcome extends CI_Controller {

    public function index()
    {
        $this->load->view('welcome_message');
    }
    public function prueba(){
        $this->load->view("prueba");
    }
    public function prueba2($v){
        echo $v;
    }
    public function submit(){
         $this->load->model('prueba_model');
         $this->prueba_model->insert_data();
        var_dump($this->input->post(null, true));
    }
}

and this is the model

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

 class Prueba_model  extends CI_Model {
   public $campoa;
   public $campob;

   function __construct(){
     parent::__construct();
   }

   public function insert_data(){
     $this->campoa = $this->input->post('title');
     $this->campob = $this->input->post('text');

     $this->db->insert('prueba',$this);
   }
 }

I get the following errors

Severity: Notice

Message: Undefined property: Welcome::$db

Filename: core/Model.php

Line Number: 73

Backtrace:

File: /home/user/htdocs/di/pruebaci/application/models/Prueba_model.php
Line: 16
Function: __get

File: /home/user/htdocs/di/pruebaci/application/controllers/Welcome.php
Line: 33
Function: insert_data

File: /home/user/htdocs/di/pruebaci/index.php

I don' t know whats is the problem I think i'm following the doc,the problem it's when I try tu use the model object on the controller. Can someone tell my what is wrong Line: 315 Function: require_once

and

An uncaught Exception was encountered

Type: Error

Message: Call to a member function insert() on null

Filename: /home/user/htdocs/di/pruebaci/application/models/Prueba_model.php

Line Number: 16

Backtrace:

File: /home/user/htdocs/di/pruebaci/application/controllers/Welcome.php
Line: 33
Function: insert_data

File: /home/user/htdocs/di/pruebaci/index.php
Line: 315
Function: require_once

Hope this help you :

First you should load database and

second load model prueba_model in welcome controller like this : /* not necessary*/

class Welcome extends CI_Controller {

  public function __construct()
  {
    $this->load->database();
    $this->load->model('prueba_model');
  }
}

for more : https://www.codeigniter.com/user_guide/database/connecting.html#manually-connecting

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