简体   繁体   中英

How to solve this error in MySql Query syntax in codeigniter?

This is The Error I am getting..

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 ' Sensor = 'sklfj', Lens = 'lkjsdf', IR = 'lkjdsf', Audio = 'kjlasf', `WDR' at line 1

UPDATE `hdtvicameras_spec`
SET `Resolution` = 'd',
 `Image` `Sensor` = 'sklfj',
 `Lens` = 'lkjsdf',
 `IR` = 'lkjdsf',
 `Audio` = 'kjlasf',
 `WDR` = 'lkjsf1',
 `ICR` = 'klasjf',
 `IP` `Rating` = 'lkjsdf',
 `Zoom` = 'ljs'
WHERE
    `product_id` = '46'

Product_model.php

$this->db->where('product_id', $product_id);
$this->db->update($tbname, $spec_array);

After var_dump($spec_array) this is what I get

array(10) { 
    ["Resolution"]=> string(5) "first" 
    ["Image Sensor"]=> string(6) "second" 
    ["Lens"]=> string(6) "third " 
    ["IR"]=> string(6) "fourth" 
    ["Audio"]=> string(6) "fifthe" 
    ["WDR"]=> string(6) "sisxth" 
    ["ICR"]=> string(5) "seven" 
    ["IP Rating"]=> string(5) "eight" 
    ["Zoom"]=> string(4) "nine" 
    ["SD Card"]=> string(3) "ten" 
}

All those key values in array are fetched from database which is specification names of any product.

looks like there is some error with space in key name. Can anybody help me solve this?

use query like this and check:

UPDATE `hdtvicameras_spec`
SET `Resolution` = 'd',
 `Image Sensor` = 'sklfj',
 `Lens` = 'lkjsdf',
 `IR` = 'lkjdsf',
 `Audio` = 'kjlasf',
 `WDR` = 'lkjsf1',
 `ICR` = 'klasjf',
 `IP Rating` = 'lkjsdf',
 `Zoom` = 'ljs'
WHERE `product_id` = '46'

Since you added quotes, for fields names having more then 1 word, its showing error.

I think you have error here:

 `Image` `Sensor` = 'sklfj', 

After image you didn't give the value to it.

Since Image Sensor is a column with space in it, therefore you are getting an error. CI is adding ` to every field with a space. It is not a good practice to use a space in your columns in MySQL.

In order to solve this problem, either you should remove space or write a query in below mentioned form and pass it in $this->db->query() .

UPDATE `hdtvicameras_spec` 
SET `Resolution` = 'd', `Image Sensor` = 'sklfj', `Lens` = 'lkjsdf', `IR` = 'lkjdsf', `Audio` = 'kjlasf', `WDR` = 'lkjsf1', `ICR` = 'klasjf', `IP Rating` = 'lkjsdf', `Zoom` = 'ljs' 
WHERE `product_id` = '46'

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