为什么我会收到ER_PARSE_ERROR的错误消息:您的SQL语法有错误;

时间:2019-09-17 10:40:12

标签: node.js express

完整错误是

这是我的index.js  ER_PARSE_ERROR:您的SQL语法有错误。检查与您的MySQL服务器版本相对应的手册,以找到在第1行的“ sada”附近使用正确的语法

const express = require('express');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
let cryptr = require('cryptr');
cryptr = new cryptr('myTotalySecretKey');
const jwt = require('jsonwebtoken');
var dir = path.join(__dirname, 'public');
app.use(express.static(dir));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static(__dirname + 'public'));
var mysql = require('mysql');
var connection = mysql.createConnection({
    host: 'localhost',
    user: 'trello',
    password: 'trello',
    database: 'trello'
});
connection.connect(function (err) {
    if (!err) {
        console.log("Database is connected");
    } else {
        console.log("Error while connecting with database");
    }
});
module.exports = connection;
app.post('/signup', (req, res) => {
    let encryptedString = cryptr.encrypt(req.body.password);
    let sql = 'insert into users(name, email, password) values ?';
    let values = [req.body.name, req.body.email, encryptedString];
    connection.query(sql, values, function (err, result) {
        if (err) throw err;
        return res.redirect('/login.html');
    });
});
app.listen(3000);

我的html表单

<form action="/signup" method="POST">
            <div class="username">
                <input type="text" placeholder="User Name" name="name"></input>
            </div>
            <div class="username">
                <input type="email" placeholder="Email" name="email" id="email"></input>
            </div>
            <div class="username">
                <input type="password" placeholder="Password" name="password" id="password"></input>
            </div>
            <div class="button-signup-login">
                <button class="add-card-btn btn">Sign Up</button>
                <a class="signup-navigator" href="login.html">already a
                    user?</a>
            </div>

        </form>

我对Node.js很陌生,所以谁能告诉我为什么会出现此错误

1 个答案:

答案 0 :(得分:2)

您必须为插入的每个值添加一个占位符:

let sql = 'insert into users(name, email, password) values (?, ?, ?)';
let values = [req.body.name, req.body.email, encryptedString];