【发布时间】:2021-03-30 13:25:52
【问题描述】:
我目前正在开发一个 html 表单,该表单允许用户输入他们的头衔、名字、姓氏、手机和电子邮件。目前,这些数据被推送到名为 userDatabase[] 的内存数据库中。
我希望能够将数据插入到我的本地 mysql 数据库中。我可以使用此代码毫无问题地连接到我的数据库。
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "user",
password: "password",
database: "user",
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
在下面的代码中,您可以看到数据正在被推送到内存数据库中。
if (currentMethod === "POST") {
// read the body of the POST request
request.on("data", function(chunk) {
requestBody += chunk.toString();
});
// determine the POST request Content-type (and log to console)
// Either: (i) application/x-www-form-urlencoded or (ii) application/json
const { headers } = request;
let ctype = headers["content-type"];
console.log("RECEIVED Content-Type: " + ctype + "\n");
// finished reading the body of the request
request.on("end", function() {
var userData = "";
// saving the user from the body to the database
if (ctype.match(new RegExp('^application/x-www-form-urlencoded'))) {
userData = querystring.parse(requestBody);
} else {
userData = JSON.parse(requestBody);
}
//**************** */
userDatabase.push(userData)
我尝试将数据插入到名为“个人”的表中,如下所示:但我收到错误错误:ER_PARSE_ERROR:您的 SQL 语法有错误;查看与您的 MariaDB 服务器版本相对应的手册,了解在 'title = 'ms'、firstname = 'Tina'、surname = 'poole'、mobile = '+3 附近使用的正确语法。 ..' 在第 1 行
con.query("INSERT INTO personal (title , firstname, surname, mobile , email ) VALUES ?", [userData], function(err, result) {
if (err) throw err;
console.log("1 record inserted");
});
【问题讨论】: