Welcome Guest, Not a member yet? Register   Sign In
CI 2.0 -> Active record -> Replacing blank with 0?
#1

[eluser]JasonS[/eluser]
I don't think I am being stupid here.

Here is my code.

Code:
$db = array (
    'content' => $this->ci->input->post('content'),
    'seo_keywords' => $this->ci->input->post('seo_keywords'),
    'seo_description' => $this->ci->input->post('seo_description'),
    'seo_title' => $this->ci->input->post('seo_title'),
);

foreach($db as $field => $value) {
    echo $value . '<br />';
    $this->ci->db->where('name', $field)->where('page_id', $page->id)->set('value', $value)->update('page_fields');
    echo $this->ci->db->last_query() . '<br />';
}

Only the content field has any input in it. This creates the following output.

Quote:Test
UPDATE `page_fields` SET `value` = 'Test' WHERE `name` = 'content' AND `page_id` = '1'

UPDATE `page_fields` SET `value` = 0 WHERE `name` = 'seo_keywords' AND `page_id` = '1'

UPDATE `page_fields` SET `value` = 0 WHERE `name` = 'seo_description' AND `page_id` = '1'

UPDATE `page_fields` SET `value` = 0 WHERE `name` = 'seo_title' AND `page_id` = '1'

Surely this should be `value` = ''?
#2

[eluser]WanWizard[/eluser]
Why?

If a field is not defined in the $_POST array, $this->input->post() returns FALSE. You then feed this value without validation into active record, where an implicit type conversion happens, since it isn't able to deal with booleans. So FALSE becomes 0.
#3

[eluser]JasonS[/eluser]
Whoops, yeah sorry my mad. I would have thought that echo would have printed out 0 instead of nothing. I think that is what through me the most.
#4

[eluser]WanWizard[/eluser]
Use var_dump() instead of echo, that way you avoid little issues like this... Wink




Theme © iAndrew 2016 - Forum software by © MyBB