我是Node.JS
的新手,所以我在views/pages/profile.ejs
中的快递ejs中创建了一个表单,如下所示:
<form role="form" action="/profile" method="post">
<div class="box-body">
<h4>Generl Details</h4>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope"></i></span>
<input type="email" class="form-control" placeholder="Email" name="email_id">
</div>
<br/>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="text" class="form-control" placeholder="First Name" name="first_name">
</div>
<br/>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="text" class="form-control" placeholder="Last Name" name="last_name">
</div>
<div class="form-group">
<label>Address</label>
<textarea class="form-control" rows="3" name="address" placeholder="Enter you Address ..."></textarea>
</div>
<div class="form-group">
<label>Gender</label>
<select class="form-control">
<option>Male</option>
<option>Female</option>
</select>
</div>
</div><!--/.col (left) -->
<!-- right column -->
<div class="box-footer">
<input type="submit" class="btn btn-primary" id="profilesubmit" value="submit">
</div>
</form>
我想在提交时将此表单插入MySql数据库。
我为所有数据库查询创建了一个dbquery / index.js。该文件如下所示:
// load up the user model
var mysql = require('mysql');
var bcrypt = require('bcrypt-nodejs');
var dbconfig = require('../config/database');
var connection = mysql.createConnection(dbconfig.connection);
connection.query('USE ' + dbconfig.database);
var express = require('express');
var app = express();
var bodyParser = require('body-parser')
/*app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
})); */
app.use(express.bodyParser()); // get information from html forms
module.exports = function(dbquery) {
app.post("/profile", function(req, res){
connection.query("SELECT * FROM users WHERE username = ?",[username], function(err, rows) {
if (err)
return done(err);
if (rows.length) {
return done(null, false, req.flash('signupMessage', 'That username is already taken.'));
} else {
// if there is no user with that username
// create the user
console.log(req.body.first_name);
var newUserMysql = {
email_id : req.body.email_id,
first_name : req.body.first_name, // use the generateHash function in our user model
last_name : req.body.last_name
};
var insertQuery = "INSERT INTO users ( email_id, first_name, last_name ) values (?,?)";
res.send(connection.query(insertQuery,[newUserMysql.email_id, newUserMysql.first_name, newUserMysql.last_name],function(err, rows) {
newUserMysql.id = rows.insertId;
return done(null, newUserMysql);
}));
}
});
});
};
这是我为将数据插入MySQL
所做的全部工作。
table
中没有添加任何内容。如果需要任何其他细节,也请告诉我。
对此的任何帮助都会很棒。
谢谢......