我有问题,而上传csv文件 [英] I have problem while uploading the csv file

查看:100
本文介绍了我有问题,而上传csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果特定月份的详细信息尚不存在,csv文件将插入新值,如果表中已存在特定月份的详细信息,则会通过上传csv文件。

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文件的最后一行在表payslip中的所有列上重复。我知道在我的查询或某事有一些错误。但我不知道什么是错的。任何人都可以帮我解决这个问题吗?

The last row of the csv file is getting repeated all over the column in the table payslip. 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";
}
}
}

?>


推荐答案

//code.google.com/p/parsecsv-for-php/rel =nofollow> PrseCSV 从上传的csv文件获取数据。

You can better use PrseCSV for getting data from the uploaded csv file.

这篇关于我有问题,而上传csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆