更新行而不是添加新行

时间:2019-09-03 02:41:23

标签: javascript php

我的问题是我想更新该行,但是当我编辑该行时,它将添加新行,并且某些数据无法检索回到数据库中。有人知道我想念什么吗?我应该添加一些循环吗?我正在使用按钮提交,而在按钮提交中,我只希望函数editGroup(更新)仅适用于同一行。这意味着,用户可以编辑行并单击按钮提交并更新行。

  

PHP更新

function editGroup(){
global $ehorsObj;
$employeeID = $_SESSION['employeeID'];
$propertyID = $_SESSION['propertyID'];
$accountID        = (isset($_POST['accountID']) ? $_POST['accountID'] : '');
$accountLedgerID  = (isset($_POST['accountLedgerID']) ? $_POST['accountLedgerID'] : '');
$accountNo        = (isset($_POST['accountNo']) ? $_POST['accountNo'] : '');
$accountName      = (isset($_POST['accountName']) ? $_POST['accountName'] : '');
//$accountMain    = (isset($_POST['accountMain']) ? $_POST['accountMain'] : '');
$openingBalance   = (isset($_POST['openingBalance']) ? $_POST['openingBalance'] : '');
$openingBalanceDate = (isset($_POST['openingBalanceDate']) ? $_POST['openingBalanceDate'] : '');
$selectable       = (isset($_POST['selectable']) ? $_POST['selectable'] : '');
$popUp            = (isset($_POST['popUp']) ? $_POST['popUp'] : '');
$accountChar      = (isset($_POST['accountChar']) ? $_POST['accountChar'] : '');
$active           = (isset($_POST['active']) ? $_POST['active'] : '');

$sqlCount   =   "SELECT COUNT(*) AS TOTAL FROM tblAccAccounts
                 WHERE accountID = '" . $accountID . "'";
$GetResult = $ehorsObj->FetchData($sqlCount, $ehorsObj->DEFAULT_PDO_CONNECTIONS);
while ($row = $GetResult->fetch()){
    $total = $row ['TOTAL'];
}
if ($total > 0) // table returned some rows
    {
        $sqlUpdate= "UPDATE tblAccAccounts 
            SET accountLedgerID = '" . $accountLedgerID . "',
                accountNo = '" . $accountNo . "',
                accountName = '" . $accountName . "',
                openingBalance = '" . $openingBalance . "',
                openingBalanceDate = '" . $openingBalanceDate . "',
                selectable = '" . $selectable . "',
                popUp = '" . $popUp . "',
                accountChar = '" . $accountChar . "',
                active = '" . $active . "'
            WHERE accountID = '".$accountID."'"; 
        $ehorsObj->ExecuteData($sqlUpdate, $ehorsObj->DEFAULT_PDO_CONNECTIONS);
    } 
    else 
    { // no such row exists.
        $sqlInsert= "INSERT INTO tblAccAccounts
                        SET accountID = '" . $accountID . "',
                        accountLedgerID = '" . $accountLedgerID . "',
                        accountNo = '" . $accountNo . "',
                        accountName = '" . $accountName . "',
                        openingBalance = '" . $openingBalance . "',
                        openingBalanceDate = '" . $openingBalanceDate . "',
                        selectable = '" . $selectable . "',
                        popUp = '" . $popUp . "',
                        accountChar = '" . $accountChar . "', 
                        active = 'y',   
                        employeeID = '" . $employeeID . "',                         
                        propertyID = '" . $propertyID . "',                         
                        dateTimeEmployee = NOW() "; 
        $ehorsObj->ExecuteData($sqlInsert, $ehorsObj->DEFAULT_PDO_CONNECTIONS);
    }}
  

JavaScript

//AJAX call for button
    $("#btnSave").kendoButton();
    var button = $("#btnSave").data("kendoButton");
    button.bind("click", function(e) {

    //var test = $("#accountLedger").data("kendoDropDownList").value();
    var accountLedgerID = $("#accountLedger").data("kendoDropDownList").value();
    var accountID = $("#accountID").val();
    var accountName = $('#accountName').val();
    var accountNo = $("#accountNo").val();
    var openingBalance = $('#openingBalance').val();
    var openingBalanceDate = $('#openingBalanceDate').val();
    //var selectable = $("input[name=selectable]:checked").val(); 
    var selectable = $('#selectable').val(); 
    var popUp = $('#popUp').val();
    //var popUp = $("input[name=popUp]:checked").val();
    var accountChar = $("#accountChar").val();

    $.ajax({
        url: "../DesignationProgramTemplate/getCoaGroup.php",
        type: "POST",
            data: {
                method: "editGroup",
                accountID: accountID,
                accountLedgerID: accountLedgerID,
                accountName: accountName,
                accountNo: accountNo,
                openingBalance: openingBalance,
                openingBalanceDate: openingBalanceDate,
                selectable: selectable,
                popUp: popUp,
                accountChar: accountChar
                },
                        success: function(){
                            KendoAlert("Data updated");
                        }
        });
    });

有人对此有意见吗?

1 个答案:

答案 0 :(得分:0)

要只更新表的行,只需要执行UPDATE查询,此后就不需要使用INSERT

这意味着无需使用这部分代码

accountID= $ehorsObj->EHORS_PK(tblAccAccounts);
$sqlAdd     =   "INSERT INTO tblAccAccounts
                SET accountID = '" . $accountID . "',
                accountLedgerID = '" . $accountLedgerID . "',
                accountNo = '" . $accountNo . "',
                accountName = '" . $accountName . "',
                openingBalance = '" . $openingBalance . "',
                openingBalanceDate = '" . $openingBalanceDate . "',
                selectable = '" . $selectable . "',
                popUp = '" . $popUp . "',
                accountChar = '" . $accountChar . "', 
                active = 'y',   
                employeeID = '" . $employeeID . "',                         
                propertyID = '" . $propertyID . "',                         
                dateTimeEmployee = NOW() ";     

$ehorsObj->ExecuteData($sqlAdd, $ehorsObj->DEFAULT_PDO_CONNECTIONS);    } 

如果您要(应该是)INSERT换一行,如果表中不存在现有行,则应该在对表执行检查后仅这样做

例如:

$sql=mysqli_query($conn, "SELECT * FROM tblAccAccounts WHERE AccountID = $accountID");

if (mysqli_num_rows($sql) > 0) // table returned some rows
{
    $sqlUpdate= <YOUR UPDATE COMMAND HERE>;
    mysqli_query($conn,$sqlUpdate);

} 
 else { // no such row exists.
    $sqlInsert=<YOUR INSERT COMMAND HERE>;
    mysqli_query($conn,$sqlInsert);
}