上传csv文件时遇到问题

时间:2011-08-04 05:51:07

标签: php mysql csv

如果特定月份的详细信息尚未出现,则csv文件将插入新值,如果表格中已存在特定月份的详细信息,则会通过上传更新表格中包含新数据的行csv文件。

csv文件的最后一行在表payslip的整个列中重复出现。我知道我的查询中存在一些错误。但我无法弄清楚出了什么问题。有人可以帮我解决这个问题吗?

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

?>

1 个答案:

答案 0 :(得分:0)

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