Welcome Guest, Not a member yet? Register   Sign In
Inserting post data
#1

[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
#2

[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');  }
...........
#3

[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
#4

[eluser]elmne[/eluser]
Thanks for that




Theme © iAndrew 2016 - Forum software by © MyBB