i'm newbie with CI and i'm using CI v.2.2.1, i want send data from my controller to model but i have some trouble here. The error said my model is undefined property
. here's my code :
Controller : 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 : 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;
}
}
is there any configuration to use model or my code is wrong? can anyone help me ? Thx
1st thing, you're not providing enough information about the error. Is the model loaded anywhere/anyhow? From the controller you load your model this way:
$this->load->model('model_crud');
Or you can preload it, by modifying your autolad.php config file
in postUser() - you're getting your post data, but don't really use it. Instead, you're passing the whole global $_POST, which may be dirty and unsafe. I'd recommend using CodeIgniter's XSS filtering when forming a data array from POST:
$data = array (
'username' => $this->input->post('username', TRUE); //TRUE identifies you're passing your data through XSS filter,
//all other elements
);
finally:
$query = $this->model_crud->insert('Users',$data);
You can do it by sending an array to your model with table columns name as key :
Controller :
$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";
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.