diff --git a/Admin/user_manage.php b/Admin/user_manage.php new file mode 100644 index 0000000000000000000000000000000000000000..267d0facddbbe4980959a22d323303d58ad81bc9 --- /dev/null +++ b/Admin/user_manage.php @@ -0,0 +1,171 @@ +<?php +include_once '../Logic/config.php'; +// 准备SQL查询 +$stmt = DB->prepare("SELECT * FROM user WHERE user_Type = ?"); +$stmt->execute(['user']); +$users = $stmt->fetchAll(PDO::FETCH_ASSOC); + +function editUserInfo($account, $password, $teleNum, $email, $userId) +{ + $sql = "UPDATE user SET account = ?, password = ?, tele_Num = ?, email = ? WHERE id = ?"; + $stmt = DB->prepare($sql); + return $stmt->execute([$account, $password, $teleNum, $email, $userId]); +} + +function addUser($studentId, $account, $password, $teleNumber, $email) +{ + // 查询学生数据库 + $stmt = DB->prepare("SELECT * FROM student"); + $stmt->execute(); + $students = $stmt->fetchAll(PDO::FETCH_ASSOC); + $found = false; + + $stmt = DB->prepare("SELECT * FROM user WHERE id = ?"); + $stmt->execute([$studentId]); + $existingUser = $stmt->fetch(PDO::FETCH_ASSOC); + if ($existingUser) { + // 学生已存在,提示用户已注册 + echo "<script>alert('Already has this user!');</script>"; + echo "<script>window.location.href = window.location.href;</script>"; + exit; + } + + foreach ($students as $student) { + if ((string)$student['id'] == $studentId) { + //在数据库中创建新用户 + $sql = "INSERT INTO user(user_Name, user_Major, enrollment_Year, email, account, password, tele_Num, id, user_Type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'user')"; + $stmt = DB->prepare($sql); + $found = true; + return $stmt->execute([$student['student_name'], $student['major'], $student['enrollment_year'], $email, $account, $password, $teleNumber, $studentId]); + break; + } + } + if (!$found) { + echo "<script> + alert('Not in the Student list!'); + window.location.href = window.location.href; // 刷新本页面 + </script>"; + exit; + } +} + +function deleteUser($studentId) +{ + $sql = "DELETE FROM user WHERE id = ?"; + $stmt = DB->prepare($sql); + return $stmt->execute([$studentId]); +} + + +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + // 检查是否存在userId参数 + $userID = isset($_POST['userId']) ? $_POST['userId'] : null; + + // 处理表单提交的数据 + if ($_POST["formType"] == "editInfoForm") { + $userId = $_POST['id']; + $account = $_POST['account']; + $password = $_POST['password']; + $telNum = $_POST['tele_Num']; + $email = $_POST['email']; + + editUserInfo($account, $password, $telNum, $email, $userId); + echo "<script>window.location.href = " . $_SERVER['PHP_SELF'] . ";</script>"; + } elseif ($_POST["formType"] == "addUserForm") { + // 接收表单数据 + $studentId = $_POST['studentId']; + $account = $_POST['account']; + $password = $_POST['password']; + $teleNumber = $_POST['tele_Num']; + $email = $_POST['email']; + + addUser($studentId, $account, $password, $teleNumber, $email); + echo "<script>window.location.href = " . $_SERVER['PHP_SELF'] . ";</script>"; + } elseif ($_POST["formType"] == "delUserForm") { + $userId = $_POST['userId']; + + deleteUser($userId); + echo "<script>window.location.href = " . $_SERVER['PHP_SELF'] . ";</script>"; + } + +} +?> +<!DOCTYPE html> +<html lang="zh-CN"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>User Management</title> + <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet"> + <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"> +</head> +<body class="bg-gray-100"> +<div class="container mx-auto p-4"> + <table class="min-w-full max-w-4xl mx-auto border-collapse"> + <thead class="bg-gray-800 text-white"> + <tr class="border-b"> + <th class="px-4 py-2 text-left rounded-tl-lg">User Name</th> + <th class="px-4 py-2 text-left">Major</th> + <th class="px-4 py-2 text-left">Enrollment Year</th> + <th class="px-4 py-2 text-left">Account</th> + <th class="px-4 py-2 text-left">Password</th> + <th class="px-4 py-2 text-left">Student ID</th> + <th class="px-4 py-2 text-left">Telephone Number</th> + <th class="px-4 py-2 text-left">Email</th> + <th class="px-4 py-2 text-left rounded-tr-lg">Operations</th> + </tr> + </thead> + <tbody> + <?php foreach ($users as $user) { ?> + <tr class="border-b"> + <td class="px-4 py-2"><?php echo htmlspecialchars($user['user_Name']) ?></td> + <td class="px-4 py-2"><?php echo htmlspecialchars($user['user_Major']) ?></td> + <td class="px-4 py-2"><?php echo htmlspecialchars($user['enrollment_Year']) ?></td> + <td class="px-4 py-2"><?php echo htmlspecialchars($user['account']) ?></td> + <td class="px-4 py-2"><?php echo htmlspecialchars($user['password']) ?></td> + <td class="px-4 py-2"><?php echo htmlspecialchars($user['id']) ?></td> + <td class="px-4 py-2"><?php echo htmlspecialchars($user['tele_Num']) ?></td> + <td class="px-4 py-2"><?php echo htmlspecialchars($user['email']) ?></td> + <td class="px-4 py-2 text-center"> + <!-- 编辑按钮 --> + <button class="edit-btn text-blue-500 hover:text-blue-700" data-id="<?php echo $user['id'] ?>" + data-type="edit"> + <i class="fas fa-edit"></i> + </button> + <!-- 删除按钮 --> + <form action="user_manage.php" method="post"> + <input type="hidden" name="userId" value="<?php echo htmlspecialchars($user['id']) ?>"> + <input type="hidden" name="formType" value="delUserForm"> + <button type="submit" class="text-red-500 hover:text-red-700 ml-2"> + <i class="fas fa-trash-alt"></i> + </button> + </form> + <!-- 增加按钮 --> + <button class="add-btn text-green-500 hover:text-green-700 ml-2" data-type="add"> + <i class="fas fa-plus-circle"></i> + </button> + </td> + </tr> + <?php } ?> + </tbody> + </table> +</div> + +<!-- 弹出框,初始时隐藏 --> +<div id="modal" + class="hidden fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full flex items-center justify-center"> + <div class="bg-white p-8 rounded-lg shadow-lg max-w-lg w-full mx-auto" id="modal-content"> + <!-- 弹出框内容 --> + </div> +</div> + +<div class="fixed bottom-0 right-0 p-4"> + <button onclick="window.location.reload()" + class="px-4 py-2 bg-gray-800 text-white rounded-md shadow-md hover:bg-gray-900 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-opacity-50"> + <i class="fas fa-sync-alt mr-2"></i> + </button> +</div> + +<script src="../Logic/user_manage.js"></script> +</body> +</html> diff --git a/Logic/user_manage.js b/Logic/user_manage.js new file mode 100644 index 0000000000000000000000000000000000000000..3fdcef9759adb8c6a0d69fc5be67d5bfa9805dea --- /dev/null +++ b/Logic/user_manage.js @@ -0,0 +1,92 @@ +document.addEventListener("DOMContentLoaded", () => { + const editButtons = document.querySelectorAll('.edit-btn'); + const addButtons = document.querySelectorAll('.add-btn'); + const modal = document.getElementById('modal'); + const modalContent = document.getElementById('modal-content'); + + // 编辑按钮点击事件 + editButtons.forEach(button => { + button.addEventListener('click', function () { + const userId = this.getAttribute('data-id'); + // 在此处根据userId获取用户数据,并填充到模态框内容中 + modalContent.innerHTML = ` + <h2 class="text-lg font-bold mb-4">Edit User Info</h2> + <form id="editUserForm" class="space-y-4" action="user_manage.php" method="post"> + <input type="hidden" name="id" value="${userId}"> + <input type="hidden" name="formType" value="editInfoForm"> + <div> + <label class="block text-sm font-medium text-gray-700" for="account">Account</label> + <input type="text" name="account" id="account" class="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"> + </div> + <div> + <label class="block text-sm font-medium text-gray-700" for="password">Password (Must at least 8 charater and include 1 Uppercase and 1 lowercase letter, and 1 special character)</label> + <input type="password" name="password" id="password" class="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500" pattern=".{8,}" title="I told you at least 8 character!"> + </div> + <div> + <label class="block text-sm font-medium text-gray-700" for="tele_Num">Telephone Number</label> + <input type="text" name="tele_Num" id="tele_Num" class="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"> + </div> + <div> + <label class="block text-sm font-medium text-gray-700" for="email">Email</label> + <input type="email" name="email" id="email" class="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"> + </div> + <div class="flex justify-end"> + <button type="submit" class="inline-flex items-center px-4 py-2 bg-blue-500 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-blue-700 active:bg-blue-900 focus:outline-none focus:border-blue-900 focus:ring focus:ring-blue-300 disabled:opacity-25 transition"> + Submit + </button> + </div> + </form> + `; + modal.classList.remove('hidden'); + }); + }); + + // 增加按钮点击事件 + addButtons.forEach(button => { + button.addEventListener('click', function () { + modalContent.innerHTML = ` + <h2 class="text-lg font-bold mb-4">Add user</h2> + <form id="addUserForm" class="space-y-4" action="user_manage.php" method="post"> + <input type="hidden" name="formType" value="addUserForm"> + <div> + <label class="block text-sm font-medium text-gray-700" for="account">Student ID</label> + <input type="text" name="studentId" id="studentId" class="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"> + </div> + <div> + <label class="block text-sm font-medium text-gray-700" for="account">Account</label> + <input type="text" name="account" id="account" class="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"> + </div> + <div> + <label class="block text-sm font-medium text-gray-700" for="password">Password</label> + <input type="password" name="password" id="password" class="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W).{8,}" + title="Password must be at least 8 characters long, including 1 uppercase letter, 1 special character." required> + </div> + <div> + <label class="block text-sm font-medium text-gray-700" for="tele_Num">Telephone Number</label> + <input type="text" name="tele_Num" id="tele_Num" class="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"> + </div> + <div> + <label class="block text-sm font-medium text-gray-700" for="email">Email</label> + <input type="email" name="email" id="email" class="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"> + </div> + <div class="flex justify-end"> + <button type="submit" class="inline-flex items-center px-4 py-2 bg-blue-500 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-blue-700 active:bg-blue-900 focus:outline-none focus:border-blue-900 focus:ring focus:ring-blue-300 disabled:opacity-25 transition"> + Submit + </button> + </div> + </form> + `; + modal.classList.remove('hidden'); + }); + }); + + // 点击模态框外区域关闭模态框 + modal.addEventListener('click', function (e) { + if (e.target === modal) { + modal.classList.add('hidden'); + } + }); + + + +});