-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent_form.php
More file actions
182 lines (162 loc) · 9.86 KB
/
Copy pathstudent_form.php
File metadata and controls
182 lines (162 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
require_once("includes/auth.php");
require_role(['admin', 'staff']);
include("config.php");
$is_edit = isset($_GET['id']);
$student_data = null;
$edit_id = $is_edit ? trim($_GET['id']) : null;
// ถ้าเป็นการแก้ไข ดึงข้อมูลเดิม (student_id เป็น varchar PK จึงใช้ prepared statement แทน intval)
if($is_edit) {
$stmt = $conn->prepare("SELECT * FROM students WHERE student_id = ?");
$stmt->bind_param('s', $edit_id);
$stmt->execute();
$student_data = $stmt->get_result()->fetch_assoc();
$stmt->close();
if(!$student_data) {
header("Location: students.php");
exit();
}
}
$page_title = $is_edit ? "แก้ไขข้อมูลนักศึกษา" : "เพิ่มนักศึกษาใหม่";
include("includes/header.php");
$errors = [];
// บันทึกข้อมูล
if($_SERVER['REQUEST_METHOD'] == 'POST') {
verify_csrf();
$student_id = trim($_POST['student_id'] ?? '');
$first_name = trim($_POST['first_name'] ?? '');
$last_name = trim($_POST['last_name'] ?? '');
$academic_year = trim($_POST['academic_year'] ?? '');
$program = trim($_POST['program'] ?? '');
$year_level = trim($_POST['year_level'] ?? '');
$class_group = trim($_POST['class_group'] ?? '');
$semester = trim($_POST['semester'] ?? '');
if(!preg_match('/^[A-Za-z0-9]{5,20}$/', $student_id)) {
$errors[] = 'รหัสนักศึกษาไม่ถูกต้อง (5-20 ตัวอักษร ตัวเลขหรือตัวอักษรภาษาอังกฤษ)';
}
if($first_name === '') { $errors[] = 'กรุณากรอกชื่อ'; }
if($last_name === '') { $errors[] = 'กรุณากรอกนามสกุล'; }
if(!ctype_digit($academic_year) || intval($academic_year) < 2500 || intval($academic_year) > 2600) {
$errors[] = 'กรุณากรอกปีการศึกษา (พ.ศ.) ให้ถูกต้อง';
}
$year_level_val = ($year_level !== '') ? intval($year_level) : null;
if(empty($errors)) {
if($is_edit) {
// รหัสนักศึกษาแก้ไขไม่ได้หลังบันทึกแล้ว (ใช้ค่าจาก URL/DB เดิมเสมอ)
$stmt = $conn->prepare(
"UPDATE students SET first_name=?, last_name=?, academic_year=?, program=?, year_level=?, class_group=?, semester=?
WHERE student_id = ?"
);
$stmt->bind_param('ssssisss', $first_name, $last_name, $academic_year, $program, $year_level_val, $class_group, $semester, $edit_id);
$stmt->execute();
$stmt->close();
flash_set('success', 'บันทึกการแก้ไขข้อมูลนักศึกษาเรียบร้อยแล้ว');
header("Location: students.php");
exit();
} else {
// student_id ห้ามซ้ำ
$stmt = $conn->prepare("SELECT student_id FROM students WHERE student_id = ?");
$stmt->bind_param('s', $student_id);
$stmt->execute();
if($stmt->get_result()->fetch_assoc()) {
$errors[] = 'รหัสนักศึกษานี้มีอยู่ในระบบแล้ว';
}
$stmt->close();
if(empty($errors)) {
$stmt = $conn->prepare(
"INSERT INTO students (student_id, first_name, last_name, academic_year, program, year_level, class_group, semester)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
);
$stmt->bind_param('sssssiss', $student_id, $first_name, $last_name, $academic_year, $program, $year_level_val, $class_group, $semester);
$stmt->execute();
$stmt->close();
flash_set('success', 'เพิ่มข้อมูลนักศึกษาเรียบร้อยแล้ว');
header("Location: students.php");
exit();
}
}
}
if(!empty($errors)) {
$student_data = compact('student_id','first_name','last_name','academic_year','program','class_group','semester');
$student_data['year_level'] = $year_level;
}
}
?>
<div class="breadcrumb">
<a href="index.php"><?php echo icon('home', ['class'=>'icon-sm']); ?> หน้าหลัก</a>
<span class="sep">/</span><a href="students.php">รายชื่อนักศึกษา</a>
<span class="sep">/</span><span class="current"><?php echo $is_edit ? 'แก้ไขข้อมูล' : 'เพิ่มนักศึกษา'; ?></span>
</div>
<div class="page-header"><div><h1><?php echo $is_edit ? 'แก้ไขข้อมูลนักศึกษา' : 'เพิ่มนักศึกษาใหม่ (กรอกแทนนักศึกษา)'; ?></h1></div></div>
<?php if(!empty($errors)): ?>
<div class="alert alert-error" role="alert">
<?php echo icon('alert-triangle'); ?>
<ul>
<?php foreach($errors as $err): ?><li><?php echo htmlspecialchars($err); ?></li><?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<div class="card">
<form method="POST" action="" data-guard-submit>
<?php echo csrf_field(); ?>
<div class="form-grid">
<div class="gc-6">
<label class="form-label" for="student_id">รหัสนักศึกษา<span class="required">*</span></label>
<input type="text" id="student_id" name="student_id" class="input<?php echo $is_edit ? ' input-readonly' : ''; ?>"
value="<?php echo $student_data ? htmlspecialchars($student_data['student_id']) : ''; ?>"
placeholder="เช่น 6501234567" required <?php echo $is_edit ? 'readonly' : ''; ?>>
</div>
<div class="gc-6">
<label class="form-label" for="academic_year">ปีการศึกษา (พ.ศ.)<span class="required">*</span></label>
<input type="text" id="academic_year" name="academic_year" class="input"
value="<?php echo $student_data ? htmlspecialchars($student_data['academic_year']) : ''; ?>"
placeholder="เช่น 2566" required>
</div>
<div class="gc-6">
<label class="form-label" for="first_name">ชื่อ<span class="required">*</span></label>
<input type="text" id="first_name" name="first_name" class="input"
value="<?php echo $student_data ? htmlspecialchars($student_data['first_name']) : ''; ?>" required>
</div>
<div class="gc-6">
<label class="form-label" for="last_name">นามสกุล<span class="required">*</span></label>
<input type="text" id="last_name" name="last_name" class="input"
value="<?php echo $student_data ? htmlspecialchars($student_data['last_name']) : ''; ?>" required>
</div>
<div class="gc-3">
<label class="form-label" for="program">สาขาวิชา</label>
<input type="text" id="program" name="program" class="input" value="<?php echo htmlspecialchars($student_data['program'] ?? ''); ?>">
</div>
<div class="gc-3">
<label class="form-label" for="year_level">ชั้นปีที่</label>
<select id="year_level" name="year_level" class="input">
<option value="">ไม่ระบุ</option>
<?php for($y=1;$y<=6;$y++): ?>
<option value="<?php echo $y; ?>" <?php echo (string)($student_data['year_level'] ?? '') === (string)$y ? 'selected' : ''; ?>>ปี <?php echo $y; ?></option>
<?php endfor; ?>
</select>
</div>
<div class="gc-3">
<label class="form-label" for="class_group">กลุ่มเรียน</label>
<input type="text" id="class_group" name="class_group" class="input" value="<?php echo htmlspecialchars($student_data['class_group'] ?? ''); ?>">
</div>
<div class="gc-3">
<label class="form-label" for="semester">ภาคการศึกษา</label>
<select id="semester" name="semester" class="input">
<option value="">ไม่ระบุ</option>
<option value="1" <?php echo ($student_data['semester'] ?? '')==='1'?'selected':''; ?>>1</option>
<option value="2" <?php echo ($student_data['semester'] ?? '')==='2'?'selected':''; ?>>2</option>
<option value="3" <?php echo ($student_data['semester'] ?? '')==='3'?'selected':''; ?>>ฤดูร้อน</option>
</select>
</div>
</div>
<div class="btn-group mt-4">
<button type="submit" class="btn btn-primary"><?php echo icon('save', ['class'=>'icon-sm']); ?> <?php echo $is_edit ? 'บันทึกการแก้ไข' : 'บันทึกข้อมูล'; ?></button>
<a href="students.php" class="btn btn-secondary">ยกเลิก</a>
</div>
</form>
</div>
<div class="alert alert-info mt-4">
<?php echo icon('info'); ?>
<span>หน้านี้ใช้สำหรับกรอกข้อมูลนักศึกษาแทนกรณีพิเศษ (ไม่มีบัญชีเข้าระบบด้วยตนเอง) รหัสนักศึกษาจะไม่สามารถแก้ไขได้หลังจากบันทึกแล้ว</span>
</div>
<?php include("includes/footer.php"); ?>