CodeIgniter Forums
escaping null values - 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: escaping null values (/showthread.php?tid=3969)

Pages: 1 2


escaping null values - El Forum - 10-31-2007

[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?


escaping null values - El Forum - 10-31-2007

[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)



escaping null values - El Forum - 10-31-2007

[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.


escaping null values - El Forum - 10-31-2007

[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


escaping null values - El Forum - 10-31-2007

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


escaping null values - El Forum - 10-31-2007

[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;
   }
}



escaping null values - El Forum - 10-31-2007

[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);
}



escaping null values - El Forum - 10-31-2007

[eluser]Pygon[/eluser]
or

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

array_walk_recursive($_POST);

Should work...


escaping null values - El Forum - 10-31-2007

[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');



escaping null values - El Forum - 10-31-2007

[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.