多次向数据库添加相同的记录而不是一次

时间:2013-10-09 14:30:24

标签: javascript php jquery sql

我有一个php脚本的问题,它添加了比它想象的更多的记录。我有一张大桌子,里面有几个“子表”,里面有人们的工资和分布情况:

姓名#PayRate#Distribution#Annual

John Smith#10,000#20.00%#50,000#edit#delete
彼得潘#100,000#100.00%#100,000#edit#delete

添加新人

当您点击新人,表单弹出时,您键入所有信息,将人员添加到数据库,ajax返回信息并将人员添加到列表的末尾。这是问题,当我点击一个“子表”添加人,它添加一个人并正确打印,但当我去第二个“子表”,并添加一个人,它将添加人两次(插入两个相同的记录到数据库),如果我点击第三个“子表”上的添加人,它将添加一个记录三次...它应该只添加一次记录。这是我的js脚本:

$('.addbtt').on('click', function(){
    value= "";
    document.getElementById("updform").reset();
    var glnumber = $(this).parent().parent().find('#glno').text();
    var objid = glnumber.substring(7,12);
    value += glnumber.substring(10,12);
    if(objid=='51001'){
        $('.modal-title').html("Add person - Full time");
        $('#glnumber').empty().val(glnumber);
    }else if(objid=='51002'){
        $('.modal-title').html("Add person - Part time");
        $('#glnumber').empty().val(glnumber);
    }else if(objid=='51009'){
        $('.modal-title').html("Add person - GA");
        $('#glnumber').empty().val(glnumber);
    }else if(objid=='51010'){
        $('.modal-title').html("Add person - GE");
        $('#glnumber').empty().val(glnumber);
    };

    $('#updform').submit(function(){
        var formData = $(this).serialize();
        $.post('includes/updatesal.php',formData,processData);
        function processData(data){
            if(value == 01){
                $('#addperson1').before(data);
            }else if(value == 02){
                $('#addperson2').before(data);
            }else if(value == 09){
                $('#addperson3').before(data);
            }else if(value == 10){
                $('#addperson4').before(data);
            }else{
                alert("Your are doing something you shouldn't! :)") 
            };
        }; //end of processData
        return false;
    });
});

我认为问题在于我的“添加人员按钮”,因为它还向数据库添加了两条记录。这是我的php函数:

$insertSQL1 = "INSERT INTO salaries (gl_number,name,pay_rate,distribution,annual)
                VALUES ('".$glnumber."','".$name."','".$payRate."','".$dist."','".$annual."')";
        $insertSQL1 .= "; SELECT SCOPE_IDENTITY() AS RECORD_ID";    
        $stmt1 = sqlsrv_query(Database::GetInstance()->databaseConnection, $insertSQL1);
        # IF THERE IS AN ERROR
        if(!$stmt1){
            echo "Error";   
        }else{
            $id = Database::GetInstance()->LastInsertId($stmt1);
        }
        # IF ALL QUERIES WERE SUCCSESSFUL, COMMIT THE TRANSACTION, OTHERWISE ROLLBACK
        if($stmt1 && !$errors){
            sqlsrv_commit(Database::GetInstance()->databaseConnection);
            # FREE THE STATEMENT
            Database::GetInstance()->FreeDBStatement($stmt1);

我现在试图解决这个问题一周,有人可以帮忙吗?

2 个答案:

答案 0 :(得分:4)

问题是由.submit()事件处理程序 .click()事件处理程序中引起的 - 所以你要创建一个新的,相同的.submit()处理程序每次点击。

要修复,只需将.submit()处理程序从那里取出:

$('.addbtt').on('click', function(){
    value= "";
    document.getElementById("updform").reset();
    var glnumber = $(this).parent().parent().find('#glno').text();
    var objid = glnumber.substring(7,12);
    value += glnumber.substring(10,12);
    if(objid=='51001'){
        $('.modal-title').html("Add person - Full time");
        $('#glnumber').empty().val(glnumber);
    }else if(objid=='51002'){
        $('.modal-title').html("Add person - Part time");
        $('#glnumber').empty().val(glnumber);
    }else if(objid=='51009'){
        $('.modal-title').html("Add person - GA");
        $('#glnumber').empty().val(glnumber);
    }else if(objid=='51010'){
        $('.modal-title').html("Add person - GE");
        $('#glnumber').empty().val(glnumber);
    };
});

$('#updform').submit(function(){
    var formData = $(this).serialize();
    $.post('includes/updatesal.php',formData,processData);
    function processData(data){
        if(value == 01){
            $('#addperson1').before(data);
        }else if(value == 02){
            $('#addperson2').before(data);
        }else if(value == 09){
            $('#addperson3').before(data);
        }else if(value == 10){
            $('#addperson4').before(data);
        }else{
            alert("Your are doing something you shouldn't! :)") 
        };
    }; //end of processData
    return false;
});

答案 1 :(得分:0)

此外,您可以将表单清除脚本移动到“提交处理程序”内部

// No need following on click handler, if button is submit
$('.addbtt').on('click', function(){

});

$('#updform').submit(function(){
value= "";
    document.getElementById("updform").reset();
    var glnumber = $(this).parent().parent().find('#glno').text();
    var objid = glnumber.substring(7,12);
    value += glnumber.substring(10,12);
    if(objid=='51001'){
        $('.modal-title').html("Add person - Full time");
        $('#glnumber').empty().val(glnumber);
    }else if(objid=='51002'){
        $('.modal-title').html("Add person - Part time");
        $('#glnumber').empty().val(glnumber);
    }else if(objid=='51009'){
        $('.modal-title').html("Add person - GA");
        $('#glnumber').empty().val(glnumber);
    }else if(objid=='51010'){
        $('.modal-title').html("Add person - GE");
        $('#glnumber').empty().val(glnumber);
    };

    var formData = $(this).serialize();
    $.post('includes/updatesal.php',formData,processData);
    function processData(data){
        if(value == 01){
            $('#addperson1').before(data);
        }else if(value == 02){
            $('#addperson2').before(data);
        }else if(value == 09){
            $('#addperson3').before(data);
        }else if(value == 10){
            $('#addperson4').before(data);
        }else{
            alert("Your are doing something you shouldn't! :)") 
        };
    }; //end of processData
    return false;
});