3

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);
Dinistro
  • 5,701
  • 1
  • 30
  • 38
Mansoorkhan Cherupuzha
  • 1,761
  • 1
  • 24
  • 45
  • This practice is one of those added on "what not to do"... [This](http://stackoverflow.com/a/14190833/1057527) might give you an idea how to do manage space in column name. – machineaddict Nov 17 '14 at 07:33

2 Answers2

2

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');
Mahendra Jella
  • 5,450
  • 1
  • 33
  • 38
0

for me ...

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

.. changed nothing! :(

«Adj Close» gets to «'Adj' 'Close'» in the query. with ' as `.

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!

Fusca Software
  • 709
  • 6
  • 11