我是一名学生,正在研究我的数据库系统项目。我的系统有不同的帐户权限,如客户、员工和管理员。我正在开发管理系统(图 1),遇到了这个问题,管理员应该能够编辑员工信息(图 2),但以某种方式更新表中每个员工的新信息。
这是我设计方法的方式: 在进入编辑页面之前,管理员帐户可以单击编辑按钮,该按钮会将行号存储到会话中(以指示将编辑哪个员工),并将直接转到另一个具有用于输入新信息的文本字段的页面。在文本字段中输入新信息并单击更新按钮后,它应该将信息更新到会话中的 WHERE emp_id=emp_id 表中(行号)。
但就像我说的,它以某种方式向表中的每个员工更新新信息,导致他们具有相同的姓名、出生日期、性别。
这是一些文件 adminmain.php:
<?php
session_start();
include 'connect.php';
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (!isset($_SESSION['admin_id'])) {
header("Location: login.php");
exit();
}
// Determine which content to show
$content = isset($_GET['content']) ? $_GET['content'] : 'profile';
if (isset($_GET['edit_emp_id'])) {
$_SESSION['edit_emp_id'] = $_GET['edit_emp_id']; // Store the employee ID in session
echo "Session ID set: " . $_SESSION['edit_emp_id']; // Debugging line
header("Location: adminmain.php?content=editemp"); // Reload the page to show edit mode
exit();
}
if (isset($_SESSION['edit_emp_id'])) {
$emp_id = $_SESSION['edit_emp_id'];
// Now, you can fetch the employee data using $emp_id
$sql = "SELECT * FROM employee WHERE emp_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $emp_id);
$stmt->execute();
$result = $stmt->get_result();
if ($row = $result->fetch_assoc()) {
// Populate the form with employee details
$emp_fname = $row['emp_fname'];
$emp_lname = $row['emp_lname'];
$emp_gender = $row['emp_gender'];
$emp_dob = $row['emp_dob'];
}
}
if ($content == 'profile') {
$sql = "SELECT * FROM admin WHERE admin_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $_SESSION['admin_id']);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$admin_fname = $row['admin_fname'];
$admin_lname = $row['admin_lname'];
$admin_gender = $row['admin_gender'];
$admin_dob = $row['admin_dob'];
$admin_pic = $row['admin_pic'];
}
elseif ($content == 'employee') {
$sql = "SELECT * FROM employee";
$result = $conn->query($sql);
}
elseif ($content == 'product') {
$sql = "SELECT * FROM product";
$result = $conn->query($sql);
}
elseif ($content == 'assignment') {
$sql = "SELECT * FROM product";
$result = $conn->query($sql);
}
elseif ($content == 'promotion') {
$sql = "SELECT * FROM product";
$result = $conn->query($sql);
}
elseif ($content == 'changepass') {
$sql = "SELECT * FROM product";
$result = $conn->query($sql);
}
elseif ($content == 'editemp') {
$sql = "SELECT * FROM product";
$result = $conn->query($sql);
}
else {
echo "Invalid content!";
exit();
}
$activeProfile = ($content == 'profile') ? 'active' : '';
$activeEmployee = ($content == 'employee') ? 'active' : '';
$activeProduct = ($content == 'product') ? 'active' : '';
$activePromotion = ($content == 'promotion') ? 'active' : '';
$activeChangepass = ($content == 'changepass') ? 'active' : '';
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" />
<link rel="stylesheet" href="css/admin.css">
</head>
<body>
<div class="container">
<aside class="sidebar">
<header class="sidebar-header">
<a href="login.php" class="header-logo">
<img src="pictures/logowall.png" alt="GrocerEase" style="width: 45px; height: auto;">
</a>
<a class="titlee">GrocerEase</a>
</header>
<nav class="sidebar-nav">
<!-- Top, Primary -->
<ul class="nav-list primary-nav">
<li class="nav-item">
<a href="adminmain.php?content=profile" class="nav-link">
<span class="nav-icon material-symbols-outlined">person</span>
<span class="nav-label">Profile</span>
</a>
</li>
<li class="nav-item">
<a href="adminmain.php?content=employee" class="nav-link">
<span class="nav-icon material-symbols-outlined">badge</span>
<span class="nav-label">Employee</span>
</a>
</li>
<li class="nav-item">
<a href="adminmain.php?content=product" class="nav-link">
<span class="nav-icon material-symbols-outlined">shopping_cart</span>
<span class="nav-label">Product</span>
</a>
</li>
<li class="nav-item">
<a href="adminmain.php?content=assignment" class="nav-link">
<span class="nav-icon material-symbols-outlined">assignment</span>
<span class="nav-label">Assignment</span>
</a>
</li>
<li class="nav-item">
<a href="adminmain.php?content=promotion" class="nav-link">
<span class="nav-icon material-symbols-outlined">percent</span>
<span class="nav-label">Promotion</span>
</a>
</li>
<li class="nav-item">
<a href="adminmain.php?content=changepass" class="nav-link">
<span class="nav-icon material-symbols-outlined">password</span>
<span class="nav-label">Change Password</span>
</a>
</li>
</ul>
<!-- Bottom, Secondary -->
<ul class="nav-list secondary-nav">
<li class="nav-item">
<a href="logout.php" class="nav-link">
<span class="nav-icon material-symbols-outlined">logout</span>
<span class="nav-label">Logout</span>
</a>
</li>
</ul>
</nav>
</aside>
<main class="profile">
<!-- Display content based on the selected menu item -->
<?php if ($content == 'profile') : ?>
<div class="profile-container">
<div class="profile-picture">
<img src="data:image/jpeg;base64,<?php echo base64_encode($admin_pic); ?>" alt="Admin Picture" class="profile-img" />
</div>
<div class="profile-info">
<p><strong>First Name:</strong> <?php echo htmlspecialchars($admin_fname); ?></p>
<p><strong>Last Name:</strong> <?php echo htmlspecialchars($admin_lname); ?></p>
<p><strong>Gender:</strong> <?php echo htmlspecialchars($admin_gender); ?></p>
<p><strong>Date of Birth:</strong> <?php echo htmlspecialchars($admin_dob); ?></p>
</div>
</div>
<?php elseif ($content == 'employee') : ?>
<!-- Display employee data here -->
<div class="employee-list">
<h2>Employee List</h2>
<?php while ($row = $result->fetch_assoc()) : ?>
<div class="employee-item">
<span class="employee-name"><strong>Name:</strong> <?php echo htmlspecialchars($row['emp_fname'] . ' ' . $row['emp_lname']); ?></span>
<span class="employee-id"><strong>ID:</strong> <?php echo htmlspecialchars($row['emp_id']); ?></span>
<span class="employee-gender"><strong>Gender:</strong> <?php echo htmlspecialchars($row['emp_gender']); ?></span>
<span class="employee-dob"><strong>Date of Birth:</strong> <?php echo htmlspecialchars(date("d-m-Y", strtotime($row['emp_dob']))); ?></span>
<span class="employee-actions">
<a href="adminmain.php?content=employee&edit_emp_id=<?php echo $row['emp_id']; ?>">Edit</a>
</span>
</div>
<?php endwhile; ?>
</div>
<?php elseif ($content == 'product') : ?>
<h2>Product List</h2>
<!-- Display product data here -->
<?php while ($row = $result->fetch_assoc()) : ?>
<p>Product Name: <?php echo $row['product_name']; ?></p>
<?php endwhile; ?>
<?php elseif ($content == 'assignment') : ?>
<h2>Assignment</h2>
<?php while ($row = $result->fetch_assoc()) : ?>
<p>Assignment Name: <?php echo $row['product_name']; ?></p>
<?php endwhile; ?>
<?php elseif ($content == 'promotion') : ?>
<h2>Promotion</h2>
<?php while ($row = $result->fetch_assoc()) : ?>
<p>Promotion Name: <?php echo $row['product_name']; ?></p>
<?php endwhile; ?>
<?php elseif ($content == 'changepass') : ?>
<h2>Change Password</h2>
<?php while ($row = $result->fetch_assoc()) : ?>
<p>Change Password: <?php echo $row['product_name']; ?></p>
<?php endwhile; ?>
<?php elseif ($content == 'editemp') : ?>
<h2>Edit Employee</h2>
<form action="update_employee.php" method="post">
<?php if (isset($_SESSION['edit_emp_id'])): ?>
<!-- You can pass the emp_id in a hidden input -->
<input type="hidden" name="edit_emp_id" value="<?php echo htmlspecialchars($_SESSION['edit_emp_id']); ?>">
<p>Employee ID: <?php echo htmlspecialchars($_SESSION['edit_emp_id']); ?></p>
<?php else: ?>
<p>Employee ID not found in session!</p>
<?php endif; ?>
<label for="emp_fname">First Name:</label>
<input type="text" id="emp_fname" name="emp_fname" value="<?php echo htmlspecialchars($emp_fname); ?>">
<label for="emp_lname">Last Name:</label>
<input type="text" id="emp_lname" name="emp_lname" value="<?php echo htmlspecialchars($emp_lname); ?>">
<label for="emp_gender">Gender:</label>
<input type="text" id="emp_gender" name="emp_gender" value="<?php echo htmlspecialchars($emp_gender); ?>">
<label for="emp_dob">Date of Birth:</label>
<input type="date" id="emp_dob" name="emp_dob" value="<?php echo htmlspecialchars($emp_dob); ?>">
<button type="submit" name="update_type" value="employee_edit">Update</button>
<button type="submit" formaction="delete_employee.php" name="update_type" value="delete">Delete</button>
</form>
<?php endif; ?>
</main>
</div>
</body>
</html>
这是我的 update_employee.php :
<?php
session_start();
include 'connect.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Determine the action or type of update
$updateType = isset($_POST['update_type']) ? $_POST['update_type'] : '';
switch ($updateType) {
case 'employee_edit':
if (!isset($_SESSION['edit_emp_id'])) {
echo "<script>alert('No employee selected for editing!');</script>";
header("Location: adminmain.php?content=employee");
exit();
}
$emp_id = $_SESSION['edit_emp_id'];
$emp_fname = $_POST['emp_fname'];
$emp_lname = $_POST['emp_lname'];
$emp_gender = $_POST['emp_gender'];
$emp_dob = $_POST['emp_dob'];
// Prepare the update query
$sql = "UPDATE employee SET emp_fname = ?, emp_lname = ?, emp_gender = ?, emp_dob = ? WHERE emp_id = ?";
$stmt = $conn->prepare($sql);
$stmt -> bind_param("ssssi",$emp_fname,$emp_lname,$emp_gender,$emp_dob,$emp_id);
$stmt->execute();
// Redirect back to the employee list
unset($_SESSION['edit_emp_id']);
header("Location: adminmain.php?content=employee");
exit();
case 'change_password':
if (!isset($_SESSION['employee_id'])) {
echo "<script>alert('No employee logged in!');</script>";
header("Location: login.php");
exit();
}
$emp_id = $_SESSION['employee_id'];
$new_password = password_hash($_POST['new_password'], PASSWORD_BCRYPT);
// Prepare the update query
$sql = "UPDATE employee SET emp_password = ? WHERE emp_id = ?";
$stmt = $conn->prepare($sql);
if ($stmt) {
$stmt->bind_param("si", $new_password, $emp_id);
if ($stmt->execute()) {
echo "<script>alert('Password updated successfully!');</script>";
} else {
echo "<script>alert('Failed to update password: " . $stmt->error . "');</script>";
}
$stmt->close();
} else {
echo "<script>alert('Failed to prepare statement: " . $conn->error . "');</script>";
}
// Redirect back to the profile page
unset($_SESSION['edit_emp_id']);
header("Location: profile.php");
exit();
default:
echo "<script>alert('Invalid update type!');</script>";
unset($_SESSION['edit_emp_id']);
header("Location: adminmain.php");
exit();
}
}
?>
如果只向 1 名员工更新新信息那就太好了
请耐心等待我的项目,因为我是二年级学生,该项目正处于早期开发阶段。抱歉我的代码很乱,英语也很糟糕哈哈哈。谢谢你
愚蠢的错误,KIKO Software 指出我使用了
$stmt -> bind_param("ssssi",$emp_fname,$emp_lname,$emp_gender,$emp_dob,$emp_id);
我将 emp_id 存储为 varchar 而不是整数,所以它应该是“sssss”。