需要有关通过csv文件上载更新mysql表的帮助

时间:2011-08-03 05:20:53

标签: php mysql csv

以下是我的脚本,用csv文件上传表员工。该文件正在上传并完美地提升员工表。但问题是我在csv文件中指定了带标题的行。该标题行也在表中更新。我想只上传那些数据,除了csv文件中的标题行,任何帮助或想法?

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

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

if(strtolower($chk_file[1]) == 'csv')
{
$filename = $_FILES['sel_file']['tmp_name'];
$handle = fopen($filename,"r");

while(($data = fgetcsv($handle,1000,",")) != false)
{
$sql = "INSERT into   employee(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]','$data[9]','$data[10]','$data[11]','$data[12]','$data[13]')";

/$upd = "UPDATE student SET   month='',tot_work_days='',lop_days='',arrear_amt='',leave_encash='' where  employee_code=''";
    mysql_query($sql) or die(mysql_error());
    }
    fclose($handle);
    echo "Successfully Imported";
}
else
{
    echo "Invalid File";
}
}

?>

2 个答案:

答案 0 :(得分:0)

在循环前调用fgetcsv一次跳过第一行:

fgetcsv($handle,1000,",");
while(($data = fgetcsv($handle,1000,",")) != false)

答案 1 :(得分:0)

您还可以将LOAD DATA INFILE MySQL语句与'IGNORE 1 LINES'子句一起使用。