Initial import
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
$page_title = 'แก้ไขข้อมูลส่วนตัว';
|
||||
require_once __DIR__ . '/../includes/functions.php';
|
||||
requireLogin();
|
||||
|
||||
if (isStudent()) {
|
||||
header('Location: ' . BASE_URL . '/index.php');
|
||||
exit;
|
||||
}
|
||||
|
||||
// Hardcoded admin has no DB record
|
||||
if ($_SESSION['user_id'] === null) {
|
||||
header('Location: ' . BASE_URL . '/index.php');
|
||||
exit;
|
||||
}
|
||||
|
||||
$id = $_SESSION['user_id'];
|
||||
$user = getUser($id);
|
||||
if (!$user) {
|
||||
echo '<div class="alert alert-danger">ไม่พบข้อมูลผู้ใช้</div>';
|
||||
exit;
|
||||
}
|
||||
|
||||
$error = '';
|
||||
$message = '';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$staff_code = trim($_POST['staff_code'] ?? '');
|
||||
$name = trim($_POST['name'] ?? '');
|
||||
$email = trim($_POST['email'] ?? '');
|
||||
$password = trim($_POST['password'] ?? '');
|
||||
|
||||
if (empty($staff_code) || empty($name) || empty($email)) {
|
||||
$error = 'กรุณากรอกข้อมูลให้ครบถ้วน';
|
||||
} else {
|
||||
try {
|
||||
$db = getDB();
|
||||
// Check if staff code is already in use by another user
|
||||
$stmt = $db->prepare("SELECT COUNT(*) FROM users WHERE staff_code = ? AND id != ?");
|
||||
$stmt->execute([$staff_code, $id]);
|
||||
if ($stmt->fetchColumn() > 0) {
|
||||
$error = 'รหัสบุคลากรนี้มีผู้ใช้อื่นใช้อยู่แล้ว';
|
||||
} else {
|
||||
if (!empty($password)) {
|
||||
$stmt = $db->prepare("UPDATE users SET staff_code=?, name=?, email=?, password=? WHERE id=?");
|
||||
$stmt->execute([$staff_code, $name, $email, password_hash($password, PASSWORD_DEFAULT), $id]);
|
||||
} else {
|
||||
$stmt = $db->prepare("UPDATE users SET staff_code=?, name=?, email=? WHERE id=?");
|
||||
$stmt->execute([$staff_code, $name, $email, $id]);
|
||||
}
|
||||
|
||||
// Update session username to match new name
|
||||
$_SESSION['username'] = $name;
|
||||
|
||||
$message = 'บันทึกการแก้ไขเรียบร้อย';
|
||||
$user = getUser($id);
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
$error = 'เกิดข้อผิดพลาด: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
require_once __DIR__ . '/../includes/header.php';
|
||||
?>
|
||||
|
||||
<div class="page-heading">
|
||||
<div>
|
||||
<h4><i class="bi bi-pencil-square"></i> แก้ไขข้อมูลส่วนตัว</h4>
|
||||
<div class="text-muted small">แก้ไขข้อมูลส่วนตัวและเปลี่ยนรหัสผ่านของคุณ</div>
|
||||
</div>
|
||||
<a href="profile.php" class="btn btn-secondary btn-sm"><i class="bi bi-arrow-left"></i> กลับ</a>
|
||||
</div>
|
||||
|
||||
<?php if ($error): ?><div class="alert alert-danger"><?= htmlspecialchars($error) ?></div><?php endif; ?>
|
||||
<?php if ($message): ?><div class="alert alert-success"><?= htmlspecialchars($message) ?></div><?php endif; ?>
|
||||
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-md-6">
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<form method="POST">
|
||||
<div class="mb-3">
|
||||
<label class="form-label">รหัสบุคลากร <span class="text-danger">*</span></label>
|
||||
<input type="text" name="staff_code" class="form-control" value="<?= htmlspecialchars($user['staff_code']) ?>" required>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">ชื่อ-สกุล <span class="text-danger">*</span></label>
|
||||
<input type="text" name="name" class="form-control" value="<?= htmlspecialchars($user['name']) ?>" required>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">อีเมล <span class="text-danger">*</span></label>
|
||||
<input type="email" name="email" class="form-control" value="<?= htmlspecialchars($user['email']) ?>" required>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">บทบาทในระบบ</label>
|
||||
<input type="text" class="form-control" value="<?= $user['role'] === 'admin' ? 'ผู้ดูแลระบบ' : 'อาจารย์ที่ปรึกษา' ?>" disabled readonly>
|
||||
<small class="text-muted">ไม่สามารถแก้ไขบทบาทด้วยตัวเองได้</small>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">รหัสผ่านใหม่ <small class="text-muted">(เว้นว่างไว้หากไม่ต้องการเปลี่ยน)</small></label>
|
||||
<input type="password" name="password" class="form-control" placeholder="กรอกรหัสผ่านใหม่">
|
||||
</div>
|
||||
<hr>
|
||||
<div class="d-flex justify-content-between">
|
||||
<button type="submit" class="btn btn-primary"><i class="bi bi-save"></i> บันทึกการเปลี่ยนแปลง</button>
|
||||
<a href="profile.php" class="btn btn-secondary">ยกเลิก</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php require_once __DIR__ . '/../includes/footer.php'; ?>
|
||||
Reference in New Issue
Block a user