【发布时间】:2022-01-24 21:05:48
【问题描述】:
我是 node.js 的新手,并且学习了好几天,现在我被这个 app.post 卡住了,它对我不起作用。如果可能的话,请让我知道如何做 app.update,这样它可能对我的学习过程有很大帮助。 敬请期待回复。
const mysql = require('mysql');
const express = require('express');
var app = express();
const bodyparser = require('body-parser');
app.use(bodyparser.urlencoded({extended: false}));
app.use(bodyparser.json());
app.listen(8000);
var mysqlconnection = mysql.createConnection(
{
host: 'localhost',
user: 'Naveen',
password: '',
database: 'employeedb',
multipleStatements: true
}
);
mysqlconnection.connect((err)=>{
if(!err)
console.log("DB connection successfull");
else
console.log('DB connection failed \n Error : '+ JSON.stringify(err, undefined, 2) );
});
app.post('/employee' ,function (req, res, next) {
Name = req.query.Name,
Empcode = req.query.Empcode,
Salary = req.query.Salary
let sql = "INSERT INTO employee (Name, Empcode, Salary) VALUES (? , ?, ?)";
mysqlconnection.query('sql, (Name, Empcode, Salary) ', (err, rows, fields) => {
if (!err)
res.send("Insert succeed");
else
console.log(err);
});
});
```
and then I get these error messages:
**PS C:\xampp\htdocs\first app> node index.js DB connection successfull Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'sql, (Name, Empcode, Salary)' at line 1
(C:\xampp\htdocs\first app\node_modules\express\lib\router\index.js:275:10) { code: 'ER_PARSE_ERROR', errno: 1064, sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'sql, (Name, Empcode, Salary)' at line 1", sqlState: '42000', index: 0, sql: 'sql, (Name, Empcode, Salary) ' }**
【问题讨论】:
-
这是一个 MySQL 查询,因为您在 MySQL 查询中遇到错误。
标签: javascript mysql node.js json post