ravi
ravi

Reputation: 1220

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?

Upvotes: 1

Views: 455

Answers (3)

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.

Upvotes: 3

A J
A J

Reputation: 4024

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'

Upvotes: 1

Abhishek Gurjar
Abhishek Gurjar

Reputation: 7476

I think you have error here:

`Image` `Sensor` = 'sklfj',

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

Upvotes: 1

Related Questions