Skip to content
Advertisement

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

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

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

Insert batch is also not working.

$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);

Advertisement

Answer

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

foreach($data as $key=>$val)
{
$this->db->set($key, $val);
}
$this->db->insert('table_name');
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement