Need help with updating a mysql table by a csv file upload
Following is my script to upload the table employee with a csv file. The file is uploading and upldating the employee table perfectly. BUT the problem is i specified a row with headings in the csv file. That heading row is also getting updated in the table. I want only those datas to get uploaded except the heading row in the csv file, any help or ideas?.
<?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";
}
}
?>
Skip the first line by calling fgetcsv
once before your loop:
fgetcsv($handle,1000,",");
while(($data = fgetcsv($handle,1000,",")) != false)
You also could use LOAD DATA INFILE MySQL statement with 'IGNORE 1 LINES' clause.
精彩评论