How to insert data using active CI record into field names with space?

How to insert data into fields with a name containing a space. eg:

$data = array(
    "First Name" => "Bob",
    "Email ID" => "bob@example.com"
);
$this->db->insert("table_name", $data);

      

Insert package also doesn't work.

$data = array(
    array(
        "First Name" => "Bob",
        "Email ID" => "bob@example.com"
    ),
    array(
        "First Name" => "Joe",
        "Email ID" => "Joe@example.com"
    )
);
$this->db->insert_batch("table_name", $data);

      

+3


source to share


2 answers


Check out the following code. This worked for me. use method$db->set



foreach($data as $key=>$val)
{
$this->db->set($key, $val);
}
$this->db->insert('table_name');

      

+2


source


for me...

foreach($data as $key=>$val)
{
$this->db->set($key, $val);
}
$this->db->insert('table_name');

      

.. changed nothing!: (

"Adj Close" goes to "'Adj' 'Close'" in the request. with how".



So, I had to do this:

foreach ($data as $k => $v) {
    if (strstr($k," ")){
       $this->db->set('`'.addslashes($k).'`', $v, false);
       unset($data[$k]);
    }
}
$returnDB = $this->db->insert($table, $data);

      

then it worked!

0


source







All Articles