Slait Slait - 1 year ago 58
MySQL Question

ER_PARSE_ERROR on update query with mysql and nodejs

I've looked at other questions similar to this, but I can't seem to find my issue or resolve it. Please help.

What I'm doing is passing input data from client side to the server so it can update a certain row(s) where the ID is matched. I have tried different ways of getting the ID passed to the query, but I am still ending up with ER_PARSE_ERROR.

Route

app.post('/configuration_materials/editsend/:id', isLoggedIn, materialpricingTable.UPDATEMaterial);


Client/jQuery and ajax function

$(function() {



$('body').on('click', '.SubmitEditMat', function() {

var materialIDedit = this.id.split('-')[1];


var clientMtrlApplication = $('#clientMtrlApplication');
var clientMtrlType = $('#clientMtrlType');
var clientMtrlSpec = $('#clientMtrlSpec');
var clientPricePerSheetORFt = $('#clientPricePerSheetORFt');
var clientSqFtofSheetORLen = $('#clientSqFtofSheetORLen');
var clientWtPerFt = $('#clientWtPerFt');
var clientWtPerItem = $('#clientWtPerItem');
var clientMtrlPricePerFt = $('#clientMtrlPricePerFt');
var clientMtrlPricePerLb = $('#clientMtrlPricePerLb');
var clientMtrlPricePerItem = $('#clientMtrlPricePerItem ');
var clientLaborHrsPerFt = $('#clientLaborHrsPerFt');
var clientLaborHrsPerItem = $('#clientLaborHrsPerItem');
var clientNotes = $('#clientNotes');

var divAddMatSuccess = $('#modifiedModalEditMat');


$.ajax({
url: '/configuration_materials/editsend/' + materialIDedit,
method: 'POST',
contentType: 'application/json',
data: JSON.stringify({

serverMtrlApplication: clientMtrlApplication.val(),
serverMtrlType: clientMtrlType.val(),
serverMtrlSpec: clientPricePerSheetORFt.val(),
serverPricePerSheetORFt: clientPricePerSheetORFt.val(),
serverSqFtofSheetORLen: clientSqFtofSheetORLen.val(),
serverWtPerFt: clientWtPerFt.val(),
serverWtPerItem: clientWtPerItem.val(),
serverMtrlPricePerFt: clientMtrlPricePerFt.val(),
serverMtrlPricePerLb: clientMtrlPricePerLb.val(),
serverMtrlPricePerItem: clientMtrlPricePerItem.val(),
serverLaborHrsPerFt: clientLaborHrsPerFt.val(),
serverLaborHrsPerItem: clientLaborHrsPerItem.val(),
serverNotes: clientNotes.val()
}),

success: function(response) {
console.log(response);


divAddMatSuccess.html('');

divAddMatSuccess.append('\
<div class="alert alert-success">\
<strong>Success! Updated: </strong> ' + clientMtrlApplication.val() + clientMtrlType.val() + clientMtrlSpec.val() +'\
</div>\
');


}


});



});
});


Server side, materialpricingTable.js with UPDATEMaterial and MySQL query

// query

exports.sqlEditMaterial = "UPDATE generictablenamepleaseignore SET ? where ID = ?;";


// UPDATEMaterial function

exports.UPDATEMaterial = function (req, res) {
var weErrored = false;

var idOfMaterial = req.params.id;


var post = {

// NAMEINDATABASE: NAMEONFORM
Application: req.body.serverMtrlApplication,
MaterialType: req.body.serverMtrlType,
MtrlSpecs: req.body.serverMtrlSpec,
PricePerSheetORFt: req.body.serverPricePerSheetORFt,
SqFtofSheetORLength: req.body.serverSqFtofSheetORLen,
WtPerFt: req.body.serverWtPerFt,
WtPerItem: req.body.serverWtPerItem,
MtrlPricePerFt: req.body.serverMtrlPricePerFt,
MtrlPricePerLb: req.body.serverMtrlPricePerLb,
MtrlPricePerItem: req.body.serverMtrlPricePerItem,
LaborHrsPerFt: req.body.serverLaborHrsPerFt,
LaborHrsPerItem: req.body.serverLaborHrsPerItem,
Notes: req.body.serverNotes
};

var sendData = post + idOfMaterial;

mySqlController.connection.query(exports.sqlEditMaterial, sendData ,
function (err) {
if (err) {
console.log(err.code);
weErrored = true; // setting the error to true, this is used to display a message on the front end

}

res.send('Success!');


});
}

Answer Source

You can't use the + operator with objects (e.g. post). In this particular case, you need to pass an array with the two values instead:

var sendData = [post, idOfMaterial];

Also, the semicolon at the end is unnecessary and is only needed when using a command-line MySQL client (or when executing multiple statements in a single query of course).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download