2

I have a MySQL table like this:

enter image description here

Below is my code from where I am trying to insert data into mysql table:

var data = {
    id          : 1,
    custname    : input.custname,
    custemail   : input.custemail,
    ordertype   : input.ordertype,
    noorder     : input.noorder,
    orderdate   : input.orderdate
};

var query = connection.query('INSERT INTO order SET ?', data,      function(err,rows) {
    if (err)
        console.log("Error inserting : %s ",err );

    res.render('createorder', {title :"Order Created Successfully"});
});

But is giving me the following error:

INSERT INTO order SET `id` = 1, `custname` = 's', `custemail` = '[email protected]', `ordertype` = 'Chalbhaja', `noorder` = 2, `orderdate` = '2015-07-24T16:07:58.684Z'
Error inserting : Error: ER_PARSE_ERROR: 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 'order SET `id` = 1, `custname` = 's', `custemail` = '[email protected]', `ordertype` =' at line 1               
3
  • Looks like your using node-mysql. You should probably set each column individually within your mysql query, ie custname = ?. I've never tried using the library this way. Commented Jul 24, 2015 at 16:27
  • I saw lot of example in this way. But it is not working for me Commented Jul 24, 2015 at 16:31
  • can you console.log(query.sql) and run that sql manually? Commented Jul 24, 2015 at 17:34

1 Answer 1

3

The order word is a keyword in MySQL. It is possible to have a table named order though. You just have to escape it in your query, like this:

var query = connection.query('INSERT INTO `order` SET ?', data, function(err,rows) {
    if (err)
        console.log("Error inserting : %s ",err );

    res.render('createorder', {title :"Order Created Successfully"});
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.