我试图用模型插入一些表单数据
这是控制器
<?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));
}
}
这是模型
<?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);
}
}
我收到以下错误
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
我不喜欢&#39;知道什么是我认为我跟随文档的问题,当我尝试使用控制器上的模型对象时,问题就出现了。 有人能说出我的错误 行:315 功能:require_once
和
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
答案 0 :(得分:1)
希望这有助于您:
首先你应该加载database
和
prueba_model
控制器中的第二个加载模型welcome
,如下所示:/* not necessary*/
class Welcome extends CI_Controller {
public function __construct()
{
$this->load->database();
$this->load->model('prueba_model');
}
}
了解更多:https://www.codeigniter.com/user_guide/database/connecting.html#manually-connecting