How to import and export employee attendance data through PHP?

PHPz
Release: 2023-09-24 17:08:02
Original
1145 people have browsed it

How to import and export employee attendance data through PHP?

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

  1. Database preparation
    First, we need to prepare a database to store employee attendance data. You can use MySQL or other relational database management systems to create an attendance data table. The fields of the attendance data table include employee ID, date, work time, off work time, etc.
  2. Create upload file form
    In the HTML page, create a file upload form for selecting the attendance data file that needs to be imported. The code is as follows:
Copy after login
  1. Write the import script
    In the PHP script to import data, you first need to connect to the database and insert the data from the CSV file into the attendance data table middle. The sample code is as follows:
query($sql); } fclose($handle); } // 关闭数据库连接 $conn->close(); // 导入成功提示 echo "导入成功"; } ?>
Copy after login

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

  1. Create an export button
    In the HTML page, create a button to export employee attendance data. The code is as follows:
导出
Copy after login
  1. Write an export script
    In the PHP script to export data, you first need to connect to the database, query the employee attendance data that needs to be exported, and save the query results into a CSV file. The sample code is as follows:
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"); ?>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!