简体   繁体   中英

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

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM