我正在尝试将表单中的数据发送到mysql数据库。我的表有三列减去id列这是表单的html:
<!DOCTYPE html>
<html lang="en">
<head>
<title>contact form</title>
</head>
<body>
<div id="page">
<form method="post" action="/myaction">
<label for="name">Enter name: </label>
<input id="name" type="text" name="name" value="<%= qs.person %>"> <br><br>
<label for="age">Enter age: </label>
<input id="age" type="number" name="age" value="<%= qs.year %>"><br><br>
<label for="department">Enter department: </label>
<input id="dept" type="text" name="department" value="<%= qs.dept %>"><br><br>
<input type="submit" value="OK">
</form>
<!--site javascript included-->
<script src="jquery-3.2.0.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="site.js"></script>
</div>
</body>
</html>
这是我的node.js代码app.js:
var http = require('http');
var mysql = require('mysql');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.set('view engine', 'ejs');
var urlencodedParser = bodyParser.urlencoded({ extended: false });
var con = mysql.createConnection({
host: "localhost",
user: "Name",
password: "XXXXXXXXXXXX",
database: "mydb"
});
app.get('/contact', function (req, res) {
res.render('contact', { qs: req.query });
});
app.post('/myaction', urlencodedParser, function (req, res) {
console.log(req.body);
var name = req.body.name;
var age = req.body.age;
var department = req.body.department;
con.connect(function (err) {
if (err) throw err;
console.log("connected");
var sql = "INSERT INTO 'customers' (name, age, department) VALUES (name, age, department)";
con.query(sql, function (err) {
if (err) throw err;
console.log("One record inserted");
});
});
res.render('contact-success', { data: req.body });
});
app.listen(3000, function () {
console.log('server running on port 3000');
});
当我在窗体中输入一些值并运行它时,会出现以下错误:
错误:er\u parse\u错误:您的sql语法有错误;检查与mysql服务器版本相对应的手册,以获得在第1行“customers”(姓名、年龄、部门)值(姓名、年龄、部门)附近使用的正确语法
我发现很难弄明白。任何帮助请!!!
1条答案
按热度按时间cqoc49vn1#
不应在表名周围添加引号。看看这个。