1

I got a Syntax error:

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.

Any ideas?

desertnaut
  • 57,590
  • 26
  • 140
  • 166

2 Answers2

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,745
  • 6
  • 26
  • 33
  • Thanks @OussamaZorgui, feel free to accept this answer if the proposed solution worked for you :) – Sean3z Feb 11 '16 at 18:32
-1
#express with sql

#for update with put req
#data= req.body

 
pool.query("INSERT INTO product set ?", data, (error, results, fields) => {
            if (error) {return callBack(error);}
            return callBack(null, results);
        });


#reqenter code here
{
  "id": "4",
  "name": "tested1",
  "description": "cool",
  "final_rating": "3",
  "price": "4",
  "image_loc": "c"
}

#############################################

#for delete

#req 
{
  "id":"1"
}

pool.query("delete from product where id=?", data, (error, results, fields) => {
        if (error) {return callBack(error);}
        return callBack(null, results);
    });

#data = req.body.id
desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the [help center](https://stackoverflow.com/help/how-to-answer). – Ethan Sep 24 '22 at 23:22