0

I have php script which runs stored procedure from sql server and enters data in table present in database. when i run php script it enters the duplicate date even same data is present in the database. i need to get rid of these duplicate data. the stored procedure gives me correct output but it is this php script which is troubling me

     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;

            }   
5
  • if($obj['field_name']='Bank Name') will always be true, most probably you want to use == there. Same for if($obj['field_name']='Bank account number'). Commented Jan 21, 2016 at 7:05
  • All lines just containing only one of these: $obj['company_code']; are completely useless. They don't do anything. Commented Jan 21, 2016 at 7:06
  • It is not the problem.when i run the script it inserts proper data into database. when i again run the script it enters same data.i have 8 different values for above query.when i run it for first time it gives me 8 rows.when run again it gives 16 rows Commented Jan 21, 2016 at 7:09
  • I know that's not THE problem, that's why I post it as a comment and not an answer. But it's A problem you have to solve too. Commented Jan 21, 2016 at 7:11
  • If you want to prevent duplicate data in subsequent runs you have to either check if the records already exist, add unique keys that prevent duplicate data or store an index of already processed source data and start the next run after that index. Commented Jan 21, 2016 at 7:12

3 Answers 3

2

There is a possibility of execution of both if conditions in your loop:

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

and

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

This might run insertion two times Tryout :)

Sign up to request clarification or add additional context in comments.

1 Comment

It is not the problem.when i run the script it inserts proper data into database. when i again run the script it enters same data.i have 8 different values for above query.when i run it for first time it gives me 8 rows.when run again it gives 16 rows
0

If both if($obj['Bank_Name']!= $obj['Bank_Name_old']) and if($obj['Bank_AcNo'] != $obj['Bank_AcNo_old']) will be true, two datasets will be added to table client_details_log.

If the if statement if($obj['field_name']='Bank account number') will be false, the variables $old and $new will not be updated so that the same data will be added to the table twice.

Maybe you have to move your sql queries inside your if($obj['field_name'] == ...) statements:

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);
}

Comments

-1

There are one or two things that could pose a problem with your code. First, as Devanshu stated you could duplicate (see my recommendation below). Second your if statements are assigning (=) instead of comparing (==). Third, you should try and use functions rather than duplicating code which can make debugging difficult.

Try something like this:

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();
    }   

}

2 Comments

Your "solution" still contains irrelevant code and logical errors. It won't work at all.
Thanks Gerald, it is only meant to be a guide along with my comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.