简体   繁体   English

上载csv文件时出现问题

[英]I have problem while uploading the csv file

The csv file will insert new values if the details for the particular month is not already present and it will update the rows with the new datas in the table if the details of the particular month is already present in the table by uploading the csv file. 如果特定月份的详细信息尚不存在,csv文件将插入新值;如果特定月份的详细信息已存在于表中(通过上传csv文件),它将使用表中的新数据更新行。

The last row of the csv file is getting repeated all over the column in the table payslip. CSV文件的最后一行在表薪资单的整个列中都重复出现。 I know there is some mistake in my query or something. 我知道查询中有错误或其他错误。 But i couldnt figure out what is wrong. 但我不知道出什么问题了。 can anyone please help me solve this issue ? 谁能帮我解决这个问题?

<?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";
}
}
}

?>

您可以更好地使用PrseCSV从上传的csv文件中获取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM