Query error in code igniter ( wrong escape )

2019-08-24 03: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.