我有这个HBS代码:
{{#each data2}}
<tr>
<td width="30%"><div class="checkbox">
<label><input type="checkbox" name="data_{{no}}" id="id_produk" value=" {{no}}">{{item}}</label>
</div></td>
<td></td>
<td width="30%"><div>
<label><input type="text" id="id_produk" name="data_{{no}}" class="form-control text-right" placeholder="Qty"></label>
</div></td>
</tr>
{{/each}}
和此路由器代码:
router.post('/save', (req, res, next) => {
var sql = "INSERT INTO transaction (transaction_id, product_id, qty) VALUES ?";
});
我想将多个数据提交到mysql数据库。我怎样才能做到这一点?
最佳答案
可能是重复的问题ref:Insert in bulk using Sequelize with Node and Express
将数据传递到数组中,读取数组并进行批量创建操作
insert into (cols1, col2) values (list1val, list1val2), (list2val, list2val2)
表格数据:
data_4:['4','9'],data_5:['5','8'],data_6:['6','7'],data_7:['7','3'],data_8: ['8','2'],data_9:['9','1']
var responseObj = {data_4: [ '4', '9' ], data_5: [ '5', '8' ], data_6: [ '6', '7' ], data_7: [ '7', '3' ], data_8: [ '8', '2' ], data_9: [ '9', '1' ] };
var tblSQL = "insert into tbl (col1, col2) values ";
var sql;
for (let key in responseObj) {
if(responseObj.hasOwnProperty(key)) {
console.log(`${key} : ${responseObj[key]}`)
let id = responseObj[key][0];
let age = responseObj[key][1];
sql = (sql!=undefined ? sql+ "," : "") + "(" + id + "," + age+ ")";
}
}
console.log (tblSQL + sql);