I'm getting this error from the block of code below when my Sql query runs:
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
sqlMessage: `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 'client, short, address, address2, city, state, zip, phone, lead, color1, col' at line 1`
The query code does work in mySql Workbench, however I do have to put quotes around client
and lead
. Is that the problem here? How do I do that in a code block like this? If that's what the real problem is. I have to use the back ticks around the query. If I put quotes around client
and lead
, it still gives the same error. So how should that syntax look?
app.put(`/client/create`, function (req, res) {
const client = req.body.client;
const short = req.body.short;
const address = req.body.address;
const address2 = req.body.address2;
const city = req.body.city;
const state = req.body.state;
const zip = req.body.zip;
const phone = req.body.phone;
const lead = req.body.lead;
const color1 = '#000';
const color2 = '#fff';
connection.getConnection(function (err, connection) {
connection.query(
`INSERT INTO clients (client, short, address, address2, city, state, zip, phone, lead, color1, color2)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[client, short, address, address2, city, state, zip, phone, lead, color1, color2],
function (error, results) {
connection.release();
if (error) throw error;
res.json(results);
console.log(`Client has been added to the database.`);
}
);
});
});