0

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

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.