Query error in code igniter ( wrong escape )

半腔热情 提交于 2020-01-06 16:21:32

问题


I try to insert a row in the table on code igniter from a Array, but something is going wrong.

That's the array:

Array
(
    [Date] => 2001-08-15
    [Number] => 962883
    [Time] => 17:40
    [Etc1] => 0
    [Etc2] => 0      
)

And this the insert:

$this->db->insert('mytable', $myarray); 

A new line is inserted, but all columns are empty! Trying to find de error, I printed the last query by

echo $this->db->last_query() ." <br>";

And I got:

INSERT INTO `mytable` (`Date`, `Number`, `Time`, `Etc1`, `Etc2`) 
VALUES 
('\02\00\00\01\0-\00\08\0-\01\05\0', '\09\06\02\08\08\03\0', '\01\07\0:\04\00\0', '\00\0', '\00\0') 

For some reason I can not get, the codeigniter ( or PHP ) is wrongly escaping the values.

Any Idea?


回答1:


Firstly your change your array like this:

$data_array = Array
       (
       'Date'=> 2001-08-15
       'Number' => 962883
       'Time' => 17:40
       'Etc1' => 0
       'Etc2' => 0      
    );

 $this->your_model->insert_data($data_array);

and inside your model write function like this

  function insert_data($data=array())
  {
      $this->db->trans_start();
      $this->db->insert('your_table_name',$data);
      $this->db->trans_complete();
      return TRUE;
  }

i hope this will solve your problem




回答2:


try this , this may help.

$this->db->set('Date', '2001-08-15', FALSE);
$this->db->set('Number', '962883', FALSE);
$this->db->set('Time', '17:40', FALSE);
$this->db->set('Etc1', '0', FALSE);
$this->db->set('Etc2', '0', FALSE);

this->db->insert('mytable');



回答3:


I tried emulating your problem and was able to get the correct SQL statement genrated.

But I am pasting here the code that worked for me:

$data = array
(
    'Date' => '2001-08-15',
    'Number' => '962883',
    'Time' => '17:40',
    'Etc1' => '0',
    'Etc2' => '0'      
);

$this->db->insert('mytable', $data);

Let me know if this works - and if not, what the error message is.



来源:https://stackoverflow.com/questions/17824013/query-error-in-code-igniter-wrong-escape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!