K20GH
K20GH

Reputation: 6271

Return POST data in CodeIgniter model?

I'm trying to get my data from a form and to send it to my model below to input the data into the database table users.

I have my controller (class Login) with the following code:

class Login extends CI_Controller {

function __construct()
{
    parent::__construct();
    $this->load->model('Login_model');      
 }

 public function index()
 {
    $data = array(
    'gid'   =>  $this->input->post('gid'),
    'name'  =>  $this->input->post('name'),
    'pic'   =>  $this->input->post('pic'),
    'link'  =>  $this->input->post('link')
);
var_dump($data);
$this->Login_model->insert_entry($data);
 }

}

In my model I have the following:

class Login_model extends CI_Model {

   public function __construct()
   {
      parent::__construct();
      $this->load->database();
   }

   function insert_entry($data)
   {
       $this->db->insert('users', $data);
    }
}

The issue I have at the moment is that I don't think my model is getting the data correctly. I've been using the URL:

domain.com/login?field1=John&field2=Dave&field3=Barry&field4=Ted

If I do a the var dump seems to return the following instead of data:

array(4) { ["gid"]=> bool(false) ["name"]=> bool(false) ["pic"]=> bool(false) ["link"]=> bool(false) }

Upvotes: 0

Views: 717

Answers (2)

RJ Spiker
RJ Spiker

Reputation: 330

In your controller, you can send the POST data through as a parameter of the insert_entry() function:

$this->Login_model->insert_entry($this->input->post());

There is also a minus sign in place of the equals sign in your first line of the insert_entry() function. You can change your insert_entry() function to something along the lines of:

function insert_entry($data) {
    $this->db->insert('users', $data);
}

NOTE: Remember to validate this data before trying to submit it to the database.

Another issue I am seeing is that you are searching the POST data, but the URL is sending GET data. There a multiple ways to solve this, the easiest of which is to check the GET stream. You could use either of the following:

$this->input->get();

or:

$this->input->get_post();

The first will check your GET data, while the second will check both the GET and POST, starting with the POST.

In your controller, assuming these fields are in your GET data, you can do the following:

$data = array(
    'gid'   =>  $this->input->get('gid'),
    'name'  =>  $this->input->get('name'),
    'pic'   =>  $this->input->get('pic'),
    'link'  =>  $this->input->get('link')
);

You can read more about this in the CodeIgniter documentation at http://ellislab.com/codeigniter/user-guide/libraries/input.html

Upvotes: 1

Matthew Gruman
Matthew Gruman

Reputation: 309

You have some issues here:

  1. There is a minus sign (-) instead of an equals sign (=) in the first line of insert_entry()
  2. Data sent via the URL is GET data (use CI's form_open(); to default to POST)
  3. Your database fields and the data in the array potentially don't match up, and you may be sending extraneous data like "submit" along with it

Upvotes: 2

Related Questions