'Having error while trying to add record mysql table while using postman
app.post('/EmployeeDB', (req, res) => {
let emp = req.body;
var ss = 'INSERT INTO `employee` (`Name`)VALUES (?) ';
connection.query(ss, (err, rows, fields) =>{
if(!err)
res.send('New employee has been added');
else
console.log(err)
})
});
Using this code to add data in mysql using nodejs and postman but having this 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 '?)' at line 1",
Solution 1:[1]
You don't seem to be providing values to the query. You are saving the request data to the "emp" variable, but not passing the data into the query. Look into how to provide data in a prepared statement.
Edit: When your server receives the data from postman you are storing it in the "emp" variable:
let emp = req.body;
But this value is never sent to the database in the query. Your query is:
"INSERT INTO `employee` (`Name`)VALUES (?)"
Here you want to insert a name to the database, however the "?" here: VALUES (?) means that you need to provide an actual value for the "Name" of the employee as a prepared statement. You need to send both the query and the data to the database. You are only sending the query.
connection.query(ss, (err, rows, fields)
As you can see you are not sending data from "emp" and not binding it either. The error you are receiving shows that the database received a "?" instead of actual data to insert to the database (shown in bold text):
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 '?)' at line 1",
You should look at how to use a prepared statement in the database connector you are using.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
