我正在尝试将表单中的数据插入数据库中,但是我想知道是否还有另一种方法可以实现这一目标。
我觉得我旋转得太多了,无法实现简单的插入
var mysql = require('mysql');
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var crypto = require('crypto');
var app = express();
app.get('/',function(req,res){
res.set({
'Access-Control-Allow-Origin' : '*'
});
return res.redirect('/public/index.html');
}).listen(3000);
console.log("Server listening at : 3000");
app.use('/public', express.static(__dirname + '/public'));
app.use( bodyParser.json() );
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
// Sign-up function starts here. . .
app.post('/sign_up' ,function(req,res){
var name = req.body.name;
var email= req.body.email;
var password = req.body.password;
var phone = req.body.phone;
var post= {name:name,email:email,password:password,phone:phone};
var con = mysql.createConnection({
host:"localhost",
user:"root",
password:" "
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO nodedb.signup set ? "+con.escape(name)+"," +con.escape(email)+"," +con.escape(password)+"," +con.escape(phone);
console.log(sql);
con.query('INSERT INTO nodedb.signup set ? ',post, function (err, result) {
if (err)
{
console.log(err);
return res.redirect('/public/Failure.html');
throw err;
}
else{
console.log("1 record inserted");
return res.redirect('/public/success.html');
}
});
});
});
我从this 那里获得了这个转义功能
如果我在con.query函数中使用sql变量,那么它将失败,我正在使用转义函数,但仍然失败,