How to import and export employee attendance data through PHP?
In the daily management of an enterprise, the import and export of employee attendance data is a very important task. The import and export function of employee attendance data can be easily realized through the PHP programming language. This article will introduce how to use PHP to achieve this function and provide specific code examples.
1. Import employee attendance data
query($sql); } fclose($handle); } // 关闭数据库连接 $conn->close(); // 导入成功提示 echo "导入成功"; } ?>
In the above code, first obtain the uploaded CSV file, then connect to the database and insert the data into the attendance data table row by row, and finally close the database connection and output a prompt that the import is successful. .
2. Export employee attendance data
导出
query($sql); // 创建CSV文件 $file = fopen("attendance.csv", "w"); // 写入表头 fputcsv($file, array("员工ID", "日期", "上班时间", "下班时间")); // 写入数据 while ($data = $result->fetch_assoc()) { fputcsv($file, array($data['employee_id'], $data['date'], $data['start_time'], $data['end_time'])); } // 关闭文件 fclose($file); // 关闭数据库连接 $conn->close(); // 下载CSV文件 header("Content-type: text/csv"); header("Content-Disposition: attachment; filename=attendance.csv"); readfile("attendance.csv"); ?>
In the above code, first connect to the database and query employee attendance data. Then create a CSV file and write the query results into the CSV file line by line. Finally, set the HTTP response headers to cause the browser to download the resulting CSV file.
Now, through the above code example, we can implement the import and export function of employee attendance data. Just add the code to the corresponding file and add the corresponding form or button to the HTML page. I hope this article can be helpful in implementing this function!
The above is the detailed content of How to import and export employee attendance data through PHP?. For more information, please follow other related articles on the PHP Chinese website!