我有以下代码包含图像上传和处理输入文本。我必须将此值插入mysql数据库。我无法在数据库中插入多个值。但我甚至可以单排价值...
我可以插入此回复
{"name":"As","age":"12","imagePic":["userPic-1473665739434.jpeg"]}
我无法插入此回复
{"name":["As","bs"],"age":["12","13"],"imagePic":["userPic-1473663782550.jpeg","userPic-1473663782628.jpeg"]}
var express = require('express');
var multer = require('multer');
var mime = require('mime');
var mysql = require('mysql');
var app = express();
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
port : 3306,
password : '',
database : 'test'
});
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... nn");
} else {
console.log("Error connecting database ... nn");
}
});
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads');
},
filename: function (req, file, callback) {
callback(null, file.fieldname + '-' + Date.now() + '.' + mime.extension(file.mimetype));
}
});
var upload = multer({ storage : storage }).array('userPic');
app.get('/completeForm.html', function(req, res){
res.sendFile(__dirname + '/' + 'completeForm.html')
});
app.post("/postFormAct", function (req, res, next) {
upload(req,res,function(err) {
/*console.log(req.body.user);
console.log(req.body.email);
console.log(req.files);*/
var imgName = [];
for(var i=0; i<req.files.length; i++) {
imgName[i] = req.files[i].filename;
}
response = {
name : req.body.user,
age : req.body.email,
imagePic : imgName
};
// res.send(JSON.stringify(response)); sample response eg: {"name":["As","bs"],"age":["12","13"],"imagePic":["userPic-1473663782550.jpeg","userPic-1473663782628.jpeg"]}
if(!err) {
connection.query('INSERT INTO nodetable SET ?',[response], function(err, result){
if(!err) {
res.send(response);
}
else{
throw err;
}
});
}
else {
throw err;
}
});
});
app.listen(3000);
下面给出的Html代码
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<form method="post" action="/postFormAct" enctype="multipart/form-data">
<input type="text" name="user"><br>
<input type="text" name="email"><br>
<input type="file" name="userPic"><br>
<div id="appendMore"></div>
<input type="submit" value="Submit"><input type="button" value="Add More Fields" id="clickBtn">
</form>
<script>
$(document).ready(function() {
$("#clickBtn").click(function(){
$("#appendMore").append('<input type="text" name="user"><br><input type="text" name="email"><br><input type="file" name="userPic"><br>');
});
});
</script>
数据库结构如下所示
请有人帮忙......
答案 0 :(得分:1)
首先,对于批量MySQL插入,请使用此语法(如此处所述How do I do a bulk insert in mySQL using node.js):
var original = {
"name":["As","bs"],
"age":["12","13"],
"imagePic":["userPic-1473663782550.jpeg","userPic-1473663782628.jpeg"]
};
将它像这样硬编码到index.js中 - 这应该正常,因为我现在用你的文件和数据库结构测试它。
进行重组(仅在硬编码工作时继续):
var myResponse = [
["As", "12", "userPic-1473663782550.jpeg" ],
["Bs", "13", "userPic-1473663782628.jpeg"]
];
到
var myResponse = [];
for (var i = 0; i < original.name.length; i++) {
myResponse.push([
original.name[i],
original.age[i],
original.imagePic[i]
]);
}
你可以这样做:
@Override
protected IVerticalRulerColumn createAnnotationRulerColumn(CompositeRuler ruler) {
AnnotationRulerColumn column = new MyAnnotationRulerColumn(VERTICAL_RULER_WIDTH, getAnnotationAccess(), this);
return column;
}