• 1
Votes
name
name Punditsdkoslkdosdkoskdo

how to send data from controller to model with CodeIgniter

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

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

name
  • 2
Teacher

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);
  • 2
Reply Report