0

最近我对 node.js 产生了兴趣,并从中学到了很多,并迅速转向了 express 框架。我尝试将我的 express 应用程序与 mysql 数据库连接并成功。现在我尝试从与 mysql 数据库连接的简单登录中进行身份验证,但我不能这样做。仍然不明白为什么会这样。下面是我使用的代码。非常欢迎帮助。

var mysql = require('mysql');

connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    database: 'express_db',
    password: '12345'
});
connection.connect();


exports.admin_login = function (req, res) {
    res.render('./admin/login', {title: 'Please provide your credentials to login'});
};


exports.admin_home = function (req, res) {
    var parse = req.body;
    var name = parse.name;
    var password = parse.password;
    var sql = "select * from user where name = " + connection.escape(name) + " and     password = " + connection.escape(password);
    connection.query(sql, function (err, results) {
        if (err) throw err;
        if (name == results[0].name && password == results[0].password) {
            req.session.regenerate(function () {
                req.session.name = results[0].id;
                res.render('./admin/home', {username: results[0].name});
            });
        }
    }); 
4

0 回答 0