Welcome Guest, Not a member yet? Register   Sign In
Active Record Inserting 0
#1

[eluser]Firstrow[/eluser]
Hello all,
I have a problem with Active Record inserting.

Code in controller:
Code:
function insert()
    {
        $data = array(
        'title' => $this->input->post('title'),
        'keywords' => $this->input->post('keywords'),
        etc...
        );

        $this->model_name->insert($data);
    }

Code in Model:

Code:
function insert($data)
    {
        $this->db->insert('settings',$data);
    }


sql:
Code:
`title` varchar(255) NOT NULL,
`keywords` varchar(255) NOT NULL,


Here is my problem:
if title or keywords in $data array are empty its inserting 0
but i need to insert real empty string not 0.

Can somebody help me?
#2

[eluser]ontguy[/eluser]
could you included your view?

I encountered this in the past, I don't recall exactly what I did to avoid it.
#3

[eluser]Firstrow[/eluser]
My view file is simple (using parser):

title: {title}
keywors: {keywords}

But the problem is not in view file. Problem is in inserting/updateing data thru Active Record
Becouse it's writing 0 in db instead of empty string.

I've wrote this function to avoid my problem:

Code:
function _delete_zero($arr)
    {
        foreach ($arr as $k => $v)
        {
            if ($v == '')
            {
                $arr[$k] = NULL;
            }
        }

        return $arr;
    }

and using it in each model function:
Code:
$data = $this->_delete_zero($data);
        $this->db->update('settings',$data);

and its worked for me. And i dont know how it will work in other Db insted of MySql.

I want to know why AR inserting 0.
#4

[eluser]ontguy[/eluser]
Try this in your view, I think that's what I did to avoid this issue:
Code:
&lt;input type="text" name="title" value="&lt;?=$this-&gt;validation->title; ?&gt;" /><br />

Another alternative to what you have is:
Code:
$data = array(
        'title' => ($this->input->post('title')) ? $this->input->post('title') : null,
        );
#5

[eluser]Firstrow[/eluser]
Thanks! thats what i need.

Code:
$data = array(
'title' => ($this->input->post('title')) ? $this->input->post('title') : null,
);




Theme © iAndrew 2016 - Forum software by © MyBB