如何修复“通过一个查询多次插入mysql表”

时间:2019-03-31 16:33:54

标签: jquery html-framework-7

$$(document).on('click', '#employee_table tr input[type="checkbox"] ', function() {



    var row = $$(this).closest("tr")[0];
    new_name = row.cells[1].innerHTML;
    new_phone = row.cells[2].innerHTML;
    new_order = row.cells[3].innerHTML;
    juice_num1 = row.cells[4].innerHTML;
    juice_num2 = row.cells[5].innerHTML;
    juice_num3 = row.cells[6].innerHTML;

    $$('#product_out')


 $$('#product_out').on('click', function() {

var sqlpost = "INSERT IGNORE INTO customers_out (name, telephone, order_num, juice1_num, juice2_num, juice3_num) VALUES (' " + new_name + " ',' " + new_phone + " ' ,' " + new_order + " ',' " + juice_num1 + " ',' " + juice_num2 + " ',' " + juice_num3 + " ')";

        connection.query(sqlpost, function(err, result) {

            app.dialog.alert('Successfull insert!');

            $$('input[type="checkbox"]').prop('checked', false);
            app.views.main.router.updateCurrentUrl('/home_employee/')

        });

    })

});

当我按下product-out按钮时,第一个数据正确地插入了customer_out。

但是当我选择另一个单元格并再次按下按钮时,它将插入多次

每当我选择带有复选框的新单元格时,它将保留时间3,4,5,6次

我检查了phpmyadmin,除第一个数据外,该数据多次累加[p]

当我按下按钮时,我只希望它一次插入数据库...

1 个答案:

答案 0 :(得分:0)

我认为问题出在employee_table点击功能。

每次单击要生成的复选框

$('#product_out').on('click', function() {功能。

所以工作流应该是这样的

点击product_out按钮。

选择选中的行(仅1)以读取其值并保存数据。

重置所有选中的行。

$('#product_out').on('click', function() {

    // Read first checked row
    var row = $('#employee_table tr input[type="checkbox"]:checked').closest("tr")[0];
    new_name = row.cells[1].innerHTML;
    new_phone = row.cells[2].innerHTML;
    new_order = row.cells[3].innerHTML;
    juice_num1 = row.cells[4].innerHTML;
    juice_num2 = row.cells[5].innerHTML;
    juice_num3 = row.cells[6].innerHTML;


    // add Detail to Database
    var sqlpost = "INSERT IGNORE INTO customers_out (name, telephone, order_num, juice1_num, juice2_num, juice3_num) VALUES (' " + new_name + " ',' " + new_phone + " ' ,' " + new_order + " ',' " + juice_num1 + " ',' " + juice_num2 + " ',' " + juice_num3 + " ')";

    connection.query(sqlpost, function(err, result) {

        app.dialog.alert('Successfull insert!');

        $$('input[type="checkbox"]').prop('checked', false);
        app.views.main.router.updateCurrentUrl('/home_employee/')

    });


    // Reset all checkbox checked
    $('#employee_table tr input[type="checkbox"]:checked').each(function() { this.checked = false; });

});