Got erro for update in node js

7 views
Skip to first unread message

Kumbhani Bhavesh

unread,
Jul 17, 2016, 7:12:58 PM7/17/16
to nodejs
i got error in update query plz check i use postman for test and this is my update query value of usercolumns [ 'name', 'price', 'duration', 'gender', 'service_category_id' ] dbValues [ 'newservice', '1200', '2016-08-13', 'male', '1' ] id 2

sqlconnect.query('UPDATE service SET ? = ?  WHERE id=?',[usercolumns,dbValues,id],function(err,uprows){

Ryan Schmidt

unread,
Jul 19, 2016, 8:43:38 AM7/19/16
to nod...@googlegroups.com

On Jul 15, 2016, at 11:51 PM, Kumbhani Bhavesh wrote:

> i got error in update query plz check i use postman for test and this is my update query value of usercolumns [ 'name', 'price', 'duration', 'gender', 'service_category_id' ] dbValues [ 'newservice', '1200', '2016-08-13', 'male', '1' ] id 2
>
> sqlconnect.query('UPDATE service SET ? = ? WHERE id=?',[usercolumns,dbValues,id],function(err,uprows){

What error did you get?

Ryan Schmidt

unread,
Jul 19, 2016, 11:18:49 AM7/19/16
to nod...@googlegroups.com
Looking at this a little closer, it looks like you're saying usercolumns is an array of column names, and dbValues is an array of values, and you're expecting "UPDATE service SET ? = ?" to set each column in the usercolumns array to the corresponding value in the dbValues array. I'm not sure what library "sqlconnect" is, or what database server you're connecting to, but I've never heard of that usage, and that might be the problem. If I were writing this, I would have written something more like:

sqlconnect.query("UPDATE service SET name=?, price=?, duration=?, gender=?, service_category_id=? WHERE id=?", ["newservice", 1200, "2016-08-13", "male", 1, 2], function (err, uprows) {...});

Note I also removed the quotes around the price (1200) and the service_category_id (1), on the assumption that those db columns are defined as numbers, not strings; maybe that was your problem.

Instead of positional parameters and a numerically-indexed array, many database servers support named parameters; you might want to look into that, as it's easier to read and harder to break when you add or remove columns as your code evolves in the future.

Reply all
Reply to author
Forward
0 new messages