一、不含文件
<form method="post" role="form" action="/admin/addDB">
//不含文件的情况
var sort = req.body.sort;
var token = req.body.token;
var title = req.body.title;
var top = req.body.top;
var note = req.body.note;
var summary = req.body.summary;
pool.getConnection(function (err, connection) {
if (err) throw err;
console.log('title:' + title);
connection.query("INSERT INTO article SET ?", {
sort: sort,
title: title,
summary: summary,
note: note,
token: token
}, function (err, result) {
if (err) throw err;
res.redirect('/admin/?sort=' + sort);
connection.release();
});
});
二、含有文件或图片
<form method="post" role="form" action="/admin/addDB" enctype="multipart/form-data">
var formidable = require('formidable');
//如果含有上传文件
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
if (err) throw err;
var sort = fields.sort,
token = fields.token,
title = fields.title,
top = fields.top,
note = fields.note,
summary = fields.summary;
res.send(files);
pool.getConnection(function (err, connection) {
if (err) throw err;
//console.log('title:' + title);
connection.query("INSERT INTO article SET ?", {
sort: sort,
title: title,
summary: summary,
note: note,
token: token
}, function (err, result) {
if (err) throw err;
res.redirect('/admin/?sort=' + sort);
connection.release();
});
});
});