How to implement employee check-in and check-out function in PHP?
In modern enterprises, managing the check-in and check-out of employees is an essential task. Traditional paper sign-in forms have been gradually replaced by various electronic sign-in systems. This article will introduce how to use PHP language to implement a simple employee check-in and check-out function.
First, we need to create a database to store employee information and check-in records. It can be implemented using MySQL or other relational databases. This article takes MySQL as an example.
First, create a database named "attendance" in MySQL, then create an employee table named "employees", and A check-in and check-out record form named "checkinout".
The structure of the employee form is as follows:
CREATE TABLE employees ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) NOT NULL, position VARCHAR(100) NOT NULL );
The structure of the check-in and check-out record form is as follows:
CREATE TABLE checkinout ( id INT PRIMARY KEY AUTO_INCREMENT, employee_id INT NOT NULL, checkin DATETIME NOT NULL, checkout DATETIME );
Next, we use PHP language to write a simple web page to implement the employee check-in and check-out function. In the index.php file, we first need to connect to the database and import the table.
<?php // 连接到数据库 $servername = "localhost"; $username = "your_username"; $password = "your_password"; $dbname = "attendance"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 导入表格 $sql = "CREATE TABLE IF NOT EXISTS employees ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) NOT NULL, position VARCHAR(100) NOT NULL )"; if ($conn->query($sql) === FALSE) { echo "创建员工表格失败: " . $conn->error; } $sql = "CREATE TABLE IF NOT EXISTS checkinout ( id INT PRIMARY KEY AUTO_INCREMENT, employee_id INT NOT NULL, checkin DATETIME NOT NULL, checkout DATETIME )"; if ($conn->query($sql) === FALSE) { echo "创建签到签退记录表格失败: " . $conn->error; } ?>
Then, we create a form to enter the employee’s information.
<form action="index.php" method="post"> <label for="name">姓名:</label> <input type="text" id="name" name="name" required> <br> <label for="position">职位:</label> <input type="text" id="position" name="position" required> <br> <input type="submit" value="添加员工"> </form>
When the "Add Employee" button is clicked, we will insert the employee information into the database.
<?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = $_POST["name"]; $position = $_POST["position"]; $sql = "INSERT INTO employees (name, position) VALUES ('$name', '$position')"; if ($conn->query($sql) === FALSE) { echo "添加员工失败: " . $conn->error; } } ?>
Next, we create a table to display employee information and sign-in and sign-out buttons.
<table> <tr> <th>ID</th> <th>姓名</th> <th>职位</th> <th>操作</th> </tr> <?php $sql = "SELECT * FROM employees"; $result = $conn->query($sql); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { echo "<tr>"; echo "<td>" . $row["id"] . "</td>"; echo "<td>" . $row["name"] . "</td>"; echo "<td>" . $row["position"] . "</td>"; echo "<td><a href='checkinout.php?employee_id=" . $row["id"] . "'>签到/签退</a></td>"; echo "</tr>"; } } else { echo "<tr><td colspan='4'>暂无员工信息</td></tr>"; } ?> </table>
Finally, we need to create the checkinout.php file to implement the employee sign-in and sign-out function.
<?php // 获取员工ID $employee_id = $_GET["employee_id"]; // 如果是签到操作 if ($_SERVER["REQUEST_METHOD"] == "POST") { date_default_timezone_set('Asia/Shanghai'); $checkin = date("Y-m-d H:i:s"); $sql = "INSERT INTO checkinout (employee_id, checkin) VALUES ('$employee_id', '$checkin')"; if ($conn->query($sql) === FALSE) { echo "签到失败: " . $conn->error; } } // 如果是签退操作 if ($_SERVER["REQUEST_METHOD"] == "PUT") { date_default_timezone_set('Asia/Shanghai'); $checkout = date("Y-m-d H:i:s"); $sql = "UPDATE checkinout SET checkout='$checkout' WHERE employee_id='$employee_id' AND checkout IS NULL ORDER BY checkin DESC LIMIT 1"; if ($conn->query($sql) === FALSE) { echo "签退失败: " . $conn->error; } } header("Location: index.php"); ?>
Through the above code, we can implement a simple employee check-in and check-out function. When an employee clicks the "Sign In/Check Out" link, the check in time and check out time will be recorded in the database, and a sign in and sign out button will be displayed in the employee list.
It should be noted that the sample code provided in this article is only a simplified version that implements the employee sign-in and sign-out function. There are many details and security considerations that need to be handled in actual projects.
The above is the detailed content of How to implement employee check-in and check-out function in PHP?. For more information, please follow other related articles on the PHP Chinese website!