Files
pongpon pitisuk 72701ad58c Initial import
2026-06-25 16:49:13 +07:00

122 lines
7.7 KiB
PHP

<?php
$page_title = 'แก้ไขนักศึกษา';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/header.php';
$id = intval($_GET['id'] ?? 0);
requireStudentAccess($id);
$student = getStudent($id);
if (!$student) {
echo '<div class="alert alert-danger">ไม่พบข้อมูลนักศึกษา</div>';
require_once __DIR__ . '/../includes/footer.php';
exit;
}
$curricula = getCurricula();
$faculties = getFacultyList();
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$student_code = trim($_POST['student_code'] ?? '');
$name_th = trim($_POST['name_th'] ?? '');
$name_en = trim($_POST['name_en'] ?? '');
$email = trim($_POST['email'] ?? '');
$curriculum_id = intval($_POST['curriculum_id'] ?? 0);
$faculty = trim($_POST['faculty'] ?? '');
$graduated_institution = trim($_POST['graduated_institution'] ?? '');
$previous_qualification = trim($_POST['previous_qualification'] ?? '');
$enrollment_year = intval($_POST['enrollment_year'] ?? date('Y'));
if (empty($student_code) || empty($name_th) || empty($curriculum_id)) {
$error = 'กรุณากรอกข้อมูลให้ครบถ้วน';
} else {
try {
$stmt = getDB()->prepare("UPDATE students SET student_code=?, name_th=?, name_en=?, email=?, faculty=?, graduated_institution=?, curriculum_id=?, enrollment_year=?, previous_qualification=? WHERE id=?");
$stmt->execute([$student_code, $name_th, $name_en, $email, $faculty, $graduated_institution, $curriculum_id, $enrollment_year, $previous_qualification ?: null, $id]);
echo "<script>window.location.href='view.php?id=$id';</script>";
exit;
} catch (PDOException $e) {
$error = 'เกิดข้อผิดพลาด: ' . $e->getMessage();
}
}
}
?>
<div class="card">
<div class="card-header bg-warning">
<h4 class="mb-0"><i class="bi bi-pencil"></i> แก้ไขข้อมูลนักศึกษา</h4>
</div>
<div class="card-body">
<?php if ($error): ?>
<div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<form method="POST" class="row g-3">
<div class="col-md-3">
<label class="form-label">รหัสนักศึกษา <span class="text-danger">*</span></label>
<input type="text" name="student_code" class="form-control" required value="<?= htmlspecialchars($student['student_code']) ?>">
</div>
<div class="col-md-5">
<label class="form-label">ชื่อ-นามสกุล (ภาษาไทย) <span class="text-danger">*</span></label>
<input type="text" name="name_th" class="form-control" required value="<?= htmlspecialchars($student['name_th']) ?>">
</div>
<div class="col-md-4">
<label class="form-label">ชื่อ-นามสกุล (ภาษาอังกฤษ)</label>
<input type="text" name="name_en" class="form-control" value="<?= htmlspecialchars($student['name_en']) ?>">
</div>
<div class="col-md-4">
<label class="form-label">อีเมล</label>
<input type="email" name="email" class="form-control" value="<?= htmlspecialchars($student['email'] ?? '') ?>">
</div>
<div class="col-md-4">
<label class="form-label">สาขาวิชา <span class="text-danger">*</span></label>
<select name="curriculum_id" class="form-select" required>
<option value="">-- เลือกสาขาวิชา --</option>
<?php foreach ($curricula as $c): ?>
<option value="<?= $c['id'] ?>" <?= $c['id'] == $student['curriculum_id'] ? 'selected' : '' ?>><?= htmlspecialchars($c['code']) ?> - <?= htmlspecialchars($c['name_th']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-4">
<label class="form-label">คณะ</label>
<select name="faculty" class="form-select">
<option value="">-- เลือกคณะ --</option>
<option value="คณะนวัตกรรมดิจิทัลเทคโนโลยี" <?= $student['faculty'] == 'คณะนวัตกรรมดิจิทัลเทคโนโลยี' ? 'selected' : '' ?>>คณะนวัตกรรมดิจิทัลเทคโนโลยี (12)</option>
<option value="คณะบริหารธุรกิจ" <?= $student['faculty'] == 'คณะบริหารธุรกิจ' ? 'selected' : '' ?>>คณะบริหารธุรกิจ (11)</option>
<option value="คณะศึกษาศาสตร์และศิลปศาสตร์" <?= $student['faculty'] == 'คณะศึกษาศาสตร์และศิลปศาสตร์' ? 'selected' : '' ?>>คณะศึกษาศาสตร์และศิลปศาสตร์ (13)</option>
<option value="คณะบัญชี" <?= $student['faculty'] == 'คณะบัญชี' ? 'selected' : '' ?>>คณะบัญชี (14)</option>
<option value="คณะนิติศาสตร์และรัฐศาสตร์" <?= $student['faculty'] == 'คณะนิติศาสตร์และรัฐศาสตร์' ? 'selected' : '' ?>>คณะนิติศาสตร์และรัฐศาสตร์ (15)</option>
</select>
</div>
<div class="col-md-4">
<label class="form-label">ชื่อสถาบันที่จบ</label>
<input type="text" name="graduated_institution" class="form-control" value="<?= htmlspecialchars($student['graduated_institution']) ?>">
</div>
<div class="col-md-4">
<label class="form-label">วุฒิเดิม</label>
<select name="previous_qualification" class="form-select">
<option value="">-- เลือกวุฒิเดิม --</option>
<?php
$quals = ['ม.6 (กศน,สกร)', 'ปวช.3', 'ปวส.2', 'ปริญญาตรีใบที่ 2'];
foreach ($quals as $q):
$sel = ($student['previous_qualification'] == $q) ? 'selected' : '';
?>
<option value="<?= htmlspecialchars($q) ?>" <?= $sel ?>><?= htmlspecialchars($q) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-4">
<label class="form-label">ปีการศึกษาที่เข้าศึกษา</label>
<select name="enrollment_year" class="form-select">
<?php for ($y = date('Y')+543; $y >= 2560; $y--): ?>
<option value="<?= $y ?>" <?= $y == $student['enrollment_year'] ? 'selected' : '' ?>><?= $y ?></option>
<?php endfor; ?>
</select>
</div>
<div class="col-12">
<button type="submit" class="btn btn-warning"><i class="bi bi-save"></i> บันทึกการแก้ไข</button>
<a href="view.php?id=<?= $id ?>" class="btn btn-secondary"><i class="bi bi-arrow-left"></i> กลับ</a>
</div>
</form>
</div>
</div>
<?php require_once __DIR__ . '/../includes/footer.php'; ?>