Welcome Guest, Not a member yet? Register   Sign In
escaping null values
#1

[eluser]ken_t[/eluser]
hi, i just started working with frameworks and im not a pro php programmer.
So i dont know if im doing something wrong.

In my application i have a function that insert at a db some values from the view input fields.
There are some int type fields at this table as for example the age field.

Im using $this->db->insert(table,values) function.

And when i leave the age input field null and submit the form, the escape function treats it as string and escape it.

Then i get a db error saying invalid value for int "".

I edited the escape function at db_driver.php to make it not escape null values. Is there another way for it?
#2

[eluser]xwero[/eluser]
You can set a default value if no value is entered before you add the values to the query.
Code:
if(!is_numeric($values['age'])){ $values['age'] = 0; }
$this->db->insert(table,$values)
#3

[eluser]Crimp[/eluser]
You can set the input to NULL for int fields:

Code:
($this->input->post('age') == '') ? ($age = NULL) : ($age = $this->input->post('age'));

If nothing is entered in the field, set to NULL, otherwise use the input value.
#4

[eluser]ken_t[/eluser]
thanks for the answers, i will have a hard work now. The app has more than 20 forms with more than 10 int type fields each. :-S
#5

[eluser]xwero[/eluser]
set the int field names in an array and loop through it, less typing more flexibility.
#6

[eluser]ken_t[/eluser]
i don't know if thats the best way for it. As i'm in hurry thats is the best i could think:

i think it will make it a little heavier than checking only the int ones, but its the faster way i could think.

Code:
$data = array(
  'name' => $this->input->post('name'),
  'age' => $this->input->post('age'),
  'something' => $this->input->post('something'),
  'something2' => $this->input->post('something2'),
);


$keys = array_keys($data);

foreach($keys as $key){
   if(empty($data[$key])){
      $data[$key] = null;
   }
}
#7

[eluser]xwero[/eluser]
Code:
$fields = array('name','age','something','something2');
$values = array();
foreach($fields as $field)
{
  $values[$field] = (!$this->input->post($field))?null:$this->input->post($field);
}
#8

[eluser]Pygon[/eluser]
or

Code:
function empty2null(&$value,$key){
if( empty($value) && $value != 0) $value = NULL;
}

array_walk_recursive($_POST);

Should work...
#9

[eluser]xwero[/eluser]
[quote author="Pygon" date="1193870010"]
Code:
function empty2null(&$value,$key){
if( empty($value) && $value != 0) $value = NULL;
}

array_walk_recursive($_POST);
[/quote]
you forgot to put your function as the second argument
Code:
array_walk_recursive($_POST,'empty2null');
#10

[eluser]ken_t[/eluser]
thx for the answers again, i think when everything gets calm here i will use the xwero one.

Since different from my example my input fields and database table fields have different names =/, its not my fault, the one that worked here before made the dbtable fields names a little complicated and not very intuitive.




Theme © iAndrew 2016 - Forum software by © MyBB