Home > Backend Development > PHP Tutorial > PHP development to build an enterprise resource planning (ERP) system with user management functions

PHP development to build an enterprise resource planning (ERP) system with user management functions

王林
Release: 2023-07-02 15:22:02
Original
1233 people have browsed it

PHP development to build an enterprise resource planning (ERP) system with user management functions

With the development of the Internet, enterprises have an increasing demand for information technology. As an integrated management software, the enterprise resource planning (ERP) system can coordinate various business activities of the enterprise and improve the efficiency and competitiveness of the enterprise. Among them, the user management function is an important part of the ERP system. This article will introduce how to use PHP to develop user management functions, and attach code examples.

  1. Design database table structure

First, we need to design the database table structure to store user information. Generally speaking, the user table contains user ID, user name, password, role ID and other fields, and the role table contains role ID and role name fields.

CREATE TABLE `users` (
  `id` INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `username` VARCHAR(50) NOT NULL,
  `password` VARCHAR(255) NOT NULL,
  `role_id` INT(11) UNSIGNED,
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE `roles` (
  `id` INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `name` VARCHAR(50) NOT NULL
);
Copy after login
  1. Create user management page

Next, we need to create a user management page, which contains functions such as user list, adding users and editing users. The user list displays information about all users and provides editing and deletion functions. The Add User page and Edit User page are used to create new users and modify user information.

<?php
// index.php

include 'db.php';

// 查询数据库,获取所有用户
$query = "SELECT users.*, roles.name as role_name FROM users LEFT JOIN roles ON users.role_id = roles.id";
$result = mysqli_query($conn, $query);

// 获取用户列表
$users = [];
while ($row = mysqli_fetch_assoc($result)) {
    $users[] = $row;
}

// 显示用户列表
foreach ($users as $user) {
    echo "ID: " . $user['id'] . "<br>";
    echo "用户名: " . $user['username'] . "<br>";
    echo "角色: " . $user['role_name'] . "<br>";
    echo "创建时间: " . $user['created_at'] . "<br>";
    echo "<a href='edit.php?id=" . $user['id'] . "'>编辑</a> ";
    echo "<a href='delete.php?id=" . $user['id'] . "'>删除</a><br><br>";
}

// 添加新用户按钮
echo "<a href='add.php'>添加用户</a>";

mysqli_close($conn);
?>
Copy after login
  1. Add user

The add user page contains a form for user name, password and role selection. After the user fills out the form, the data is submitted and the new user information is inserted into the database.

<?php
// add.php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    include 'db.php';
  
    // 获取表单数据
    $username = $_POST['username'];
    $password = $_POST['password'];
    $role_id = $_POST['role_id'];
  
    // 插入新用户数据到数据库
    $query = "INSERT INTO users (username, password, role_id) VALUES ('$username', '$password', $role_id)";
    mysqli_query($conn, $query);
  
    mysqli_close($conn);
    header('Location: index.php');
    exit;
}
?>

<form method="POST" action="">
  <label for="username">用户名</label>
  <input type="text" name="username" id="username" required><br>
  
  <label for="password">密码</label>
  <input type="password" name="password" id="password" required><br>
  
  <label for="role_id">角色</label>
  <select name="role_id" id="role_id">
    <option value="1">管理员</option>
    <option value="2">普通用户</option>
  </select><br>
  
  <input type="submit" value="添加用户">
</form>
Copy after login
  1. Edit user

The edit user page is similar to the add user page, but the original user information needs to be displayed in the form and the user information in the database needs to be updated.

<?php
// edit.php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    include 'db.php';
  
    // 获取表单数据
    $id = $_POST['id'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $role_id = $_POST['role_id'];
  
    // 更新用户数据
    $query = "UPDATE users SET username='$username', password='$password', role_id=$role_id WHERE id=$id";
    mysqli_query($conn, $query);
  
    mysqli_close($conn);
    header('Location: index.php');
    exit;
}

include 'db.php';

// 获取用户ID
$id = $_GET['id'];

// 查询数据库,获取用户信息
$query = "SELECT * FROM users WHERE id=$id";
$result = mysqli_query($conn, $query);
$user = mysqli_fetch_assoc($result);

mysqli_close($conn);
?>

<form method="POST" action="">
  <!-- 显示原来的用户信息 -->
  <input type="hidden" name="id" value="<?php echo $user['id']; ?>">
  
  <label for="username">用户名</label>
  <input type="text" name="username" id="username" value="<?php echo $user['username']; ?>" required><br>
  
  <label for="password">密码</label>
  <input type="password" name="password" id="password" value="<?php echo $user['password']; ?>" required><br>
  
  <label for="role_id">角色</label>
  <select name="role_id" id="role_id">
    <option value="1" <?php if ($user['role_id'] === 1) echo 'selected'; ?>>管理员</option>
    <option value="2" <?php if ($user['role_id'] === 2) echo 'selected'; ?>>普通用户</option>
  </select><br>
  
  <input type="submit" value="保存修改">
</form>
Copy after login

Through the above code examples, we can implement PHP development of enterprise resource planning (ERP) systems with user management functions. This system can easily manage user information, including the display of user lists, the functions of adding users and editing users. Developers can expand and optimize the code according to actual needs to achieve more complex user management functions.

The above is the detailed content of PHP development to build an enterprise resource planning (ERP) system with user management functions. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template