我是 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) ' }**
uj5u.com熱心網友回復:
您的查詢中有錯字。
// WRONG
mysqlconnection.query('sql, (Name, Empcode, Salary) ', (err, rows, fields)...
它不應該在引號內,變數應該是一個陣列。
// CORRECT
mysqlconnection.query(sql, [Name, Empcode, Salary], (err, rows, fields)...
uj5u.com熱心網友回復:
錯誤很明顯。它說“您的 SQL 語法有錯誤”。你應該在“near 'sql, (Name, Empcode, Salary)'”處檢查你的語法。這條線在這里
mysqlconnection.query('sql, (Name, Empcode, Salary) ',
您正在傳遞字串文字“sql, (Name, Empcode, Salary)”,而您打算傳遞sql您在上一行中創建的變數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/399822.html
標籤:javascript mysql 节点.js json 邮政
