0

I have a Node.JS application that is deployed with Heroku featuring a simple CRUD system. However, I am having problems whenever I update a row with the web application itself.

Here is my code for the UPDATE statement in my server.js file

app.post('/logs/edit/:mobile', function(req, res){
   var input = JSON.parse(JSON.stringify(req.body));
   var mobile = req.params.mobile;
   var data = {            
        last    : input.last,
        first   : input.first,
        middle  : input.middle,
        mobile  : input.mobile,
        email   : input.email,
        bday    : input.bday,
        status  : input.status
   };
   connection.query("UPDATE logs set ? WHERE mobile = ? ",[data,mobile], function(err, rows){  
      if(err) console.log(err);
      res.redirect('/logs');          
   });
});

Then here is the error showing in my logs

2017-06-27T02:09:55.662620+00:00 app[web.1]: { 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 ''Doe', 'Doe', 'Doe', `first` = 'John', 'John', `middle` = 'Mike', 'Mike', `mobil' at line 1
2017-06-27T02:09:55.662637+00:00 app[web.1]:     at Query.Sequence._packetToError (/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:48:14)
2017-06-27T02:09:55.662639+00:00 app[web.1]:     at Query.ErrorPacket (/app/node_modules/mysql/lib/protocol/sequences/Query.js:83:18)
2017-06-27T02:09:55.662640+00:00 app[web.1]:     at Protocol._parsePacket (/app/node_modules/mysql/lib/protocol/Protocol.js:280:23)
2017-06-27T02:09:55.662641+00:00 app[web.1]:     at Parser.write (/app/node_modules/mysql/lib/protocol/Parser.js:73:12)
2017-06-27T02:09:55.662642+00:00 app[web.1]:     at Protocol.write (/app/node_modules/mysql/lib/protocol/Protocol.js:39:16)
2017-06-27T02:09:55.662642+00:00 app[web.1]:     at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:96:28)
2017-06-27T02:09:55.662644+00:00 app[web.1]:     at emitOne (events.js:96:13)
2017-06-27T02:09:55.662644+00:00 app[web.1]:     at Socket.emit (events.js:188:7)
2017-06-27T02:09:55.662646+00:00 app[web.1]:     at readableAddChunk (_stream_readable.js:176:18)
2017-06-27T02:09:55.662646+00:00 app[web.1]:     at Socket.Readable.push (_stream_readable.js:134:10)
2017-06-27T02:09:55.662648+00:00 app[web.1]:     --------------------
2017-06-27T02:09:55.662648+00:00 app[web.1]:     at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:141:48)
2017-06-27T02:09:55.662649+00:00 app[web.1]:     at Connection.query (/app/node_modules/mysql/lib/Connection.js:201:25)
2017-06-27T02:09:55.662650+00:00 app[web.1]:     at /app/server.js:59:16
2017-06-27T02:09:55.662650+00:00 app[web.1]:     at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2017-06-27T02:09:55.662651+00:00 app[web.1]:     at next (/app/node_modules/express/lib/router/route.js:131:13)
2017-06-27T02:09:55.662652+00:00 app[web.1]:     at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
2017-06-27T02:09:55.662653+00:00 app[web.1]:     at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2017-06-27T02:09:55.662653+00:00 app[web.1]:     at /app/node_modules/express/lib/router/index.js:277:22
2017-06-27T02:09:55.662654+00:00 app[web.1]:     at param (/app/node_modules/express/lib/router/index.js:349:14)
2017-06-27T02:09:55.662654+00:00 app[web.1]:     at param (/app/node_modules/express/lib/router/index.js:365:14)
2017-06-27T02:09:55.662655+00:00 app[web.1]:   code: 'ER_PARSE_ERROR',
2017-06-27T02:09:55.662656+00:00 app[web.1]:   errno: 1064,
2017-06-27T02:09:55.662657+00:00 app[web.1]:   sqlState: '42000',
2017-06-27T02:09:55.662657+00:00 app[web.1]:   index: 0 }

I hope there is a solution for this one.

Many thanks

mayk
  • 1
  • 2

1 Answers1

0

For what I know, there are MySQL versions that won't work if you send them a JSON like you're doing with data. I don't actually know what is wrong with your code but, for what I see your node code is ok.

I recommend you to try that query directly on mysql (by using the console or workbench)

Rafael del Rio
  • 208
  • 2
  • 11
  • Hi Rafael, I'm not entirely sure whether the MySQL version that I'm using with Heroku is compatible with the code that I did but it does work perfectly when I run it on my local machine through WAMP. I did try to query that as well on MySQL Workbench and it does work. – mayk Jun 28 '17 at 01:30