1

Hi i got a weird Syntax error the error is : You have an error in your SQL syntax, check the manual that corresponds to your MySQL server version for the right sybtax to use near :data at line 1. errno: 1064 SqlState: '42000'

This is The code to insert into the database:

function addCategory(category){
    var execution = q.defer();
    var query = 'INSERT INTO categories SET :data';
    console.log(query);
    connection.query(query,{data:category}, function(err, res){
        if(err){
            execution.reject(err);
            console.log(err);
            return;
        }
        execution.resolve(res);
    });
    return execution.promise;
}

While this function will get a category as a json object. the weird thing is that this function worked before and it was written the same way as the one giving the error. i would be so thankful if you can help me with the problem, thanks.

1 Answers1

1

The query you're trying to run is in invalid; check out the INSERT syntax documentation. A standard INSERT query looks similar to the following:

INSERT INTO tbl_name (col1,col2) VALUES(15,col1*2);

So in your case, you'd want to update your query to something like:

var query = 'INSERT INTO categories (category) VALUES(:data)';

(substite category for your column name, of course)

Sean3z
  • 3,585
  • 6
  • 25
  • 32
  • Thanks @OussamaZorgui, feel free to accept this answer if the proposed solution worked for you :) – Sean3z Feb 11 '16 at 18:32