Tenho um problema ao carregar o arquivo csv

O arquivo csv inserirá novos valores se os detalhes do mês em particular ainda não estiverem presentes e atualizará as linhas com os novos dados na tabela se os detalhes do mês em particular já estiverem presentes na tabela fazendo o upload do arquivo csv .

A última linha do arquivo csv está sendo repetida em toda a coluna da folha de pagamento da tabela. Eu sei que há algum erro na minha consulta ou algo assim. Mas eu não conseguia descobrir o que está errado. alguém pode me ajudar a resolver esse problema?

<?php
require_once '../config.php';

if(isset($_POST['upload']))
{
$fname = $_FILES['sel_file']['name'];
$month = $_POST['month'];
$chk_file = explode(".",$fname);

if(strtolower($chk_file[1]) == 'csv')
{
//$sel=mysql_query("select * from employee where month='$month'");
//$del=mysql_query("delete from employee where month='$month'");
$query1 = mysql_query("SELECT * FROM payslips where month='$month'");
$pay_num_rows = mysql_num_rows($query1);
    $filename = $_FILES['sel_file']['tmp_name'];
    $handle = fopen($filename,"r");
    fgetcsv($handle,1000,",");
    if($pay_num_rows > 1)
    {
    while(($data = fgetcsv($handle,1000,",")) != false)
    {
    $upd = "UPDATE payslips SET    month='$data[9]',tot_work_days='$data[10]',lop_days='$data[11]',arrear_amt='$data[12]',leave_encash='$data[13]' where month='$month'";
    mysql_query($upd) or die(mysql_error());
    }
    fclose($handle);
    echo "Successfully Imported";
}
  if($pay_num_rows == 0)
{
while(($data = fgetcsv($handle,1000,",")) != false)
    {
    $sql = "INSERT into   payslips(employee_code,employee_name,employee_address,emp_dateofjoin,emp_designation,emp_hq,pf_num,esic_num,emp_state,month,tot_work_days,lop_days,arrear_amt,leave_encash) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$month','$data[10]','$data[11]','$data[12]','$data[13]')";

    //$upd = "UPDATE employee SET  month='$data[9]',tot_work_days='$data[10]',lop_days='$data[11]',arrear_amt='$data[12]',leave_encash='$data[13]' where month='$month'";
    mysql_query($sql) or die(mysql_error());
    }
    fclose($handle);
    echo "Successfully Imported";

}
else
 {
    echo "Invalid File";
}
}
}

?>

questionAnswers(1)

yourAnswerToTheQuestion