0

I have the following php code to insert data to the table

$fields=$this->db->list_fields('branddistribution_products');
    foreach($data as $key=>$v){
        if(!in_array($key, $fields)){
            unset($data[$key]);
        }
    }

    $this->db->where('product_id', $data['product_id']);
    $this->db->from('branddistribution_products');
    $count = $this->db->count_all_results();
    if($count==0){
        $this->db->insert('branddistribution_products', $data);
    }else{
        $this->db->update('branddistribution_products', $data);
    }

and $data is an array which have picture 1 picture 2 picture 3 key indexes.But when I insert to the db the key index change to 'picture' 1, 'picture' 2, 'picture' 3.I have DB table column same as that indexes.The following database error arise

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1, `picture` 2, `picture` 3, `madein`, `Firme`, `heel`, `lenght`, `mainmaterial`' at line 1

How can I skip this index changes.

sharif779
  • 174
  • 2
  • 10
  • Not a solution for your issue, but you can smarten up your filtering step with: [array_diff_key()](https://www.php.net/manual/en/function.array-diff-key.php) Do you _actually_ have spaces in your table column names? ...a recipe for lots of future headaches. Fix your column names, not your code. I also recommend REPLACE INTO https://stackoverflow.com/a/34243189/2943403 – mickmackusa Sep 14 '19 at 11:32
  • Thank you @mickmackusa.I think so to change column name. – sharif779 Sep 14 '19 at 11:36

0 Answers0