![](/img/trans.png)
[英]How to send data from controller to model with CodeIgniter through functions
[英]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;
}
}
有沒有使用模型的配置,或者我的代碼錯誤? 誰能幫我 ? 謝謝
第一件事,您沒有提供有關錯誤的足夠信息。 模型是否在任何地方/以任何方式加載? 從控制器以這種方式加載模型:
$this->load->model('model_crud');
或者您可以通過修改autolad.php配置文件來預加載它
在postUser()中-您正在獲取帖子數據,但實際上並沒有使用它。 相反,您要傳遞整個全局$ _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);
您可以通過將表列名稱作為鍵將數組發送到模型來實現:
控制器:
$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";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.