CodeIgniter Forums
Inserting post data - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Inserting post data (/showthread.php?tid=31104)



Inserting post data - El Forum - 06-06-2010

[eluser]elmne[/eluser]
If i want to insert post data into the database but exclude empty fields (POSTS) that a user may not have filled in, how do i achieve that?

Validation cannot stop this, as it checks only for values that are a must to fill in, but if teh user doesn't have to fill in things like all address fields and certain other fields, then these should not be input.

How do i check each POST field within the model for this?

I tried this below

Code:
if (!empty($this->input->post('title'))) { $this->title  = $this->input->post('title');  }          
if (!empty($this->input->post('firstname') )) {$this->firstname = $this->input->post('firstname');  }          
if (!empty($this->input->post('middlename') )) {$this->middlename = $this->input->post('middlename');  }
...........


but I got this error message
Quote:Fatal error: Can't use method return value in write context



Inserting post data - El Forum - 06-06-2010

[eluser]Armchair Samurai[/eluser]
RTM: empty()

You can only use variables with empty() - anything else will throw an error. If you want to do this, maybe best to loop through the post array then remove anything that has no value.

Alternately:
Code:
if ( ! empty($_POST['title'])) { $this->title  = $this->input->post('title');  }          
if ( ! empty($_POST['firstname'])) {$this->firstname = $this->input->post('firstname');  }          
if ( ! empty($_POST['middlename'])) {$this->middlename = $this->input->post('middlename');  }
...........



Inserting post data - El Forum - 06-07-2010

[eluser]vitoco[/eluser]
$this->input->post('index') return FALSE when the 'index' is not set... so
Code:
if (!is_bool($this->input->post('title'))) { $this->title  = $this->input->post('title');  }          
if (!is_bool($this->input->post('firstname') )) {$this->firstname = $this->input->post('firstname');  }          
if (!is_bool($this->input->post('middlename') )) {$this->middlename = $this->input->post('middlename');  }
...........

Saludos


Inserting post data - El Forum - 06-07-2010

[eluser]elmne[/eluser]
Thanks for that