gpt4 book ai didi

mysql - 从 hbs 到 MySQL Express js 的多次插入

转载 作者:行者123 更新时间:2023-11-29 15:13:28 24 4
gpt4 key购买 nike

我有这个 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数据库。我怎样才能实现这一目标?

最佳答案

可能是重复的问题引用: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);

关于mysql - 从 hbs 到 MySQL Express js 的多次插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59878534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com