php脚本在数据库中输入重复数据

时间:2016-01-21 06:51:06

标签: php sql-server

我有php脚本,它从sql server运行存储过程,并在数据库中的表中输入数据。当我运行php脚本时,它进入重复日期,即使数据库中存在相同的数据。 我需要摆脱这些重复的数据。 存储过程给了我正确的输出,但是这个PHP脚本让我很烦恼

     while ($obj = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC  ))
{

     if($obj['Bank_Name']!= $obj['Bank_Name_old'])
            {       
            $obj['company_code']; 
            $obj['Account_Code'];
            $obj['Bank_Name']; 
            $obj['Bank_Name_old'];
            $obj['field_name']='Bank Name';

             if($obj['field_name']='Bank Name')
            {   
                $old=$obj['Bank_Name_old'];     
                $new=$obj['Bank_Name']; 
            }
                    $query="insert into vns_db.dbo.client_details_log (company_code,client_id,field_name,original_value,new_value) values ('".$obj['company_code']."',
                            '".$obj['Account_Code']."','".$obj['field_name']."','$old','$new')";    
                            $res = sqlsrv_query($conn,$query);
                            //$obj['modified_fields']=$obj['field_name'].'|'.addslashes('$old').'|'.addslashes('$new');

                        //  echo $query;
            }   
        if($obj['Bank_AcNo'] != $obj['Bank_AcNo_old'])
            {
                     $obj['company_code']; 
                     $obj['Account_Code'];
                     $obj['Bank_AcNo']; 
                     $obj['Bank_AcNo_old'];
                     $obj['field_name']='Bank account number';

                        if($obj['field_name']='Bank account number')
            {   
                    $old=$obj['Bank_AcNo_old']; 
                    $new=$obj['Bank_AcNo'];         
            }
                     $query="insert into vns_db.dbo.client_details_log (company_code,client_id,field_name,original_value,new_value) values ('".$obj['company_code']."',
                            '".$obj['Account_Code']."','".$obj['field_name']."','$old','$new')"; 
                            $res = sqlsrv_query($conn,$query);
                            //$obj['modified_fields']=$obj['field_name'].'|'.addslashes('$old').'|'.addslashes('$new');
                            //echo $query;

            }   

3 个答案:

答案 0 :(得分:2)

如果循环中的条件有可能同时执行:

if($obj['Bank_Name']!= $obj['Bank_Name_old'])

if($obj['Bank_AcNo'] != $obj['Bank_AcNo_old'])

这可能会插入两次 试用:)

答案 1 :(得分:0)

如果if($obj['Bank_Name']!= $obj['Bank_Name_old'])if($obj['Bank_AcNo'] != $obj['Bank_AcNo_old'])都是true,则会将两个数据集添加到表client_details_log

如果if语句if($obj['field_name']='Bank account number')false,则不会更新变量$old$new,以便将相同的数据添加到表中两次。

也许你必须在你的if($obj['field_name'] == ...)语句中移动你的SQL查询:

if($obj['field_name'] == 'Bank Name')
{
    $old=$obj['Bank_Name_old'];     
    $new=$obj['Bank_Name']; 

    $query="insert into vns_db.dbo.client_details_log (company_code,client_id,field_name,original_value,new_value) values ('".$obj['company_code']."', '".$obj['Account_Code']."','".$obj['field_name']."','$old','$new')";

    $res = sqlsrv_query($conn,$query);
}

答案 2 :(得分:-1)

有一两件事可能会对您的代码造成问题。首先,正如Devanshu所说,你可以复制(见下面的推荐)。其次,if语句分配(=)而不是比较(==)。第三,你应该尝试使用函数而不是复制可能使调试变得困难的代码。

尝试这样的事情:

function addData()
{
    $query = "insert into vns_db.dbo.client_details_log (company_code,client_id,field_name,original_value,new_value) values ('".$obj['company_code']."',
            '".$obj['Account_Code']."','".$obj['field_name']."','".$old."','".$new."')";    
    return sqlsrv_query($conn,$query);
}

while ($obj = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC  ))
{

     if ( ($obj['Bank_Name']!= $obj['Bank_Name_old']) || ($obj['Bank_AcNo'] != $obj['Bank_AcNo_old']) )
    {       
        $obj['company_code']; 
        $obj['Account_Code'];
        $obj['Bank_Name']; 
        $obj['Bank_Name_old'];
        $obj['field_name']='Bank Name';

         if ($obj['field_name']=='Bank Name')
        {   
            $old=$obj['Bank_Name_old'];     
            $new=$obj['Bank_Name']; 
        }

        if ($obj['field_name']=='Bank account number')
        {   
            $old=$obj['Bank_AcNo_old']; 
            $new=$obj['Bank_AcNo'];         
        }       

        $res = addData();
    }   

}