Thank you in advance for taking a look at this question! So, I am attempting to INSERT a row of data into a table named raw_base.
Here is the code:
const express = require('express');
const router = express.Router();
const mysql = require('mysql');
// Import MySQL Options
const options = require('../db_options');
const connection = mysql.createConnection(options);
router.post('/raw', (req, res) => {
let data = [
`${connection.escape(req.body[0].opened)}`,
`${connection.escape(req.body[0].funding_source)}`,
`${connection.escape(req.body[0].replace_existing_device)}`,
`${connection.escape(req.body[0].project)}`,
`${connection.escape(req.body[0].department)}`,
`${connection.escape(req.body[0].ritm_number)}`,
`${connection.escape(req.body[0].item)}`,
`${connection.escape(req.body[0].category)}`,
`${connection.escape(req.body[0].quantity)}`,
`${connection.escape(req.body[0].price)}`,
`${connection.escape(req.body[0].closed)}`
];
connection.query('INSERT INTO `raw_base` (`opened`, `funding_source`, `replace_existing_device`, `project`, `department`, `ritm_number`, `item`, `category`, `quantity`, `price`, `closed`) VALUES ?', [data], (error, results, fields) => {
if (error) throw error;
console.log(results);
});
As such, I am receiving the following error:
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 ''\'2018-07-26 13:34:33\'', '127548298', '0', '0', '\'Psychiatry Admin-Central\'', '' at line 1
If; however, I do not escape the values in the data array and add single quotes around the values in the sql INSERT query it works fine (like this):
connection.query('INSERT INTO `raw_base` (`opened`, `funding_source`, `replace_existing_device`, `project`, `department`, `ritm_number`, `item`, `category`, `quantity`, `price`, `closed`) VALUES ('
+ '\'' + req.body[0].opened + '\', '
+ '\'' + req.body[0].funding_source + '\', '
+ '\'' + req.body[0].replace_existing_device + '\', '
+ '\'' + req.body[0].project + '\', '
+ '\'' + req.body[0].department + '\', '
+ '\'' + req.body[0].ritm_number + '\', '
+ '\'' + req.body[0].item + '\', '
+ '\'' + req.body[0].category + '\', '
+ '\'' + req.body[0].quantity + '\', '
+ '\'' + req.body[0].price + '\', '
+ '\'' + req.body[0].closed + '\')'
, (error, results, fields) => {
if (error) throw error;
console.log(results);
});
I have also attempted to add single quotes around around each value in the data array with no luck. I assume this is a simple syntactical issue, but I can't seem to place my finger on exactly where I am going wrong. Thanks again for helping out!
Here is are the values from the data array (from req.body[0]):
[ '\'2018-07-26 13:34:33\'', '127548298', '0', '0', '\'Psychiatry Admin-Central\'', '\'RITM0023102\'', '\'HP USB Keyboard\'', '\'Accessories\'', '6', '14', '\'2018-08-22 12:51:40\'' ]
[ '2018-07-26 13:34:33', '275829', '0', '0', 'Psychiatry Admin-Central', ... ]. But for some reason the data contains escaped strings, as in'\'2018-07-26 13:34:33\''. You need to fix that problem before using the data.escapecan only work on what is already there. The problem isn't thatescapeis escaping single quotes; it is that you have literal single quotes inside your strings. Why, I don't know.