如何使用 CodeIgniter 将数据从控制器发送到模型
how to send data from controller to model with CodeIgniter
我是 CI 的新手,我正在使用 CI v.2.2.1,我想将数据从我的控制器发送到模型,但我在这里遇到了一些麻烦。错误说我的模型是 undefined property
。这是我的代码:
控制器:
users.php
public function postUser(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$email = $this->input->post('email');
$phone = $this->input->post('phone');
$address = $this->input->post('address');
$level = $this->input->post('level');
$status = $this->input->post('status');
$this->load->model('model_crud');
$query = $this->model_crud->insert('Users',$_POST);
echo "$query";
}
型号:
model_crud.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_crud extends CI_Model {
public function __construct(){
parent::__construct();
}
public function insert($table,$data){
$query = $this->db->insert($table,$data);
return $query;
}
}
是否有使用模型的配置或我的代码有误?
谁能帮我 ?谢谢
您可以通过使用 table 列名称作为键将数组发送到您的模型来实现:
控制器:
$data = array(
'username' => $this->input->post('username') ,
'password ' => $this->input->post('password') ,
...
);
// 'TABLE COLUMN NAME' => "VALUE"
$query = $this->model_crud->insert('Users',$data);
echo "$query";
第一件事,您没有提供足够的错误信息。
模型是否已加载 anywhere/anyhow?从控制器以这种方式加载模型:
$this->load->model('model_crud');
或者您可以通过修改 autolad.php 配置文件来预加载它
in postUser() - 您正在获取 post 数据,但并不真正使用它。相反,您正在传递整个全局 $_POST,这可能是脏的和不安全的。我建议在从 POST:
形成数据数组时使用 CodeIgniter 的 XSS 过滤
$data = array (
'username' => $this->input->post('username', TRUE); //TRUE identifies you're passing your data through XSS filter,
//all other elements
);
终于:
$query = $this->model_crud->insert('Users',$data);
我是 CI 的新手,我正在使用 CI v.2.2.1,我想将数据从我的控制器发送到模型,但我在这里遇到了一些麻烦。错误说我的模型是 undefined property
。这是我的代码:
控制器: users.php
public function postUser(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$email = $this->input->post('email');
$phone = $this->input->post('phone');
$address = $this->input->post('address');
$level = $this->input->post('level');
$status = $this->input->post('status');
$this->load->model('model_crud');
$query = $this->model_crud->insert('Users',$_POST);
echo "$query";
}
型号: model_crud.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_crud extends CI_Model {
public function __construct(){
parent::__construct();
}
public function insert($table,$data){
$query = $this->db->insert($table,$data);
return $query;
}
}
是否有使用模型的配置或我的代码有误? 谁能帮我 ?谢谢
您可以通过使用 table 列名称作为键将数组发送到您的模型来实现:
控制器:
$data = array(
'username' => $this->input->post('username') ,
'password ' => $this->input->post('password') ,
...
);
// 'TABLE COLUMN NAME' => "VALUE"
$query = $this->model_crud->insert('Users',$data);
echo "$query";
第一件事,您没有提供足够的错误信息。 模型是否已加载 anywhere/anyhow?从控制器以这种方式加载模型:
$this->load->model('model_crud');
或者您可以通过修改 autolad.php 配置文件来预加载它
in postUser() - 您正在获取 post 数据,但并不真正使用它。相反,您正在传递整个全局 $_POST,这可能是脏的和不安全的。我建议在从 POST:
形成数据数组时使用 CodeIgniter 的 XSS 过滤$data = array (
'username' => $this->input->post('username', TRUE); //TRUE identifies you're passing your data through XSS filter,
//all other elements
);
终于:
$query = $this->model_crud->insert('Users',$data);