-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternship_form.php
More file actions
173 lines (152 loc) · 8.81 KB
/
Copy pathinternship_form.php
File metadata and controls
173 lines (152 loc) · 8.81 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
<?php
require_once("includes/auth.php");
require_role(['admin', 'staff']);
include("config.php");
$is_edit = isset($_GET['id']);
$internship_data = null;
$edit_id = $is_edit ? intval($_GET['id']) : null;
// ถ้าเป็นการแก้ไข ดึงข้อมูลเดิม
if($is_edit) {
$stmt = $conn->prepare("SELECT * FROM internships WHERE internship_id = ?");
$stmt->bind_param('i', $edit_id);
$stmt->execute();
$internship_data = $stmt->get_result()->fetch_assoc();
$stmt->close();
if(!$internship_data) {
header("Location: internships.php");
exit();
}
}
$page_title = $is_edit ? "แก้ไขข้อมูลการฝึกงาน" : "เพิ่มข้อมูลการฝึกงาน";
include("includes/header.php");
$errors = [];
$statuses = ['กำลังฝึกงาน', 'ฝึกงานเสร็จสิ้น', 'ยกเลิกการฝึกงาน'];
// บันทึกข้อมูล
if($_SERVER['REQUEST_METHOD'] == 'POST') {
verify_csrf();
$student_id = trim($_POST['student_id'] ?? '');
$company_id = intval($_POST['company_id'] ?? 0);
$start_date = trim($_POST['start_date'] ?? '');
$end_date = trim($_POST['end_date'] ?? '');
$status = trim($_POST['status'] ?? '');
if($student_id === '') { $errors[] = 'กรุณาเลือกนักศึกษา'; }
if($company_id <= 0) { $errors[] = 'กรุณาเลือกบริษัท'; }
if($start_date === '' || $end_date === '') {
$errors[] = 'กรุณากรอกวันที่เริ่มและวันที่สิ้นสุดการฝึกงาน';
} elseif(strtotime($end_date) < strtotime($start_date)) {
$errors[] = 'วันที่สิ้นสุดต้องไม่มาก่อนวันที่เริ่มฝึกงาน';
}
if(!in_array($status, $statuses, true)) {
$errors[] = 'กรุณาเลือกสถานะให้ถูกต้อง';
}
if(empty($errors)) {
if($is_edit) {
$stmt = $conn->prepare(
"UPDATE internships SET student_id=?, company_id=?, start_date=?, end_date=?, status=?
WHERE internship_id=?"
);
$stmt->bind_param('sisssi', $student_id, $company_id, $start_date, $end_date, $status, $edit_id);
$stmt->execute();
$stmt->close();
flash_set('success', 'บันทึกการแก้ไขข้อมูลการฝึกงานเรียบร้อยแล้ว');
header("Location: internships.php");
exit();
} else {
$stmt = $conn->prepare(
"INSERT INTO internships (student_id, company_id, start_date, end_date, status)
VALUES (?, ?, ?, ?, ?)"
);
$stmt->bind_param('sisss', $student_id, $company_id, $start_date, $end_date, $status);
$stmt->execute();
$stmt->close();
flash_set('success', 'เพิ่มข้อมูลการฝึกงานเรียบร้อยแล้ว');
header("Location: internships.php");
exit();
}
}
if(!empty($errors)) {
$internship_data = compact('student_id','company_id','start_date','end_date','status');
}
}
// ดึงรายชื่อนักศึกษา
$students_result = mysqli_query($conn, "SELECT * FROM students ORDER BY student_id ASC");
// ดึงรายชื่อบริษัท
$companies_result = mysqli_query($conn, "SELECT * FROM companies ORDER BY company_name ASC");
?>
<div class="breadcrumb">
<a href="index.php"><?php echo icon('home', ['class'=>'icon-sm']); ?> หน้าหลัก</a>
<span class="sep">/</span><a href="internships.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>
<select id="student_id" name="student_id" class="input" required>
<option value="">เลือกนักศึกษา</option>
<?php while($student = mysqli_fetch_assoc($students_result)): ?>
<option value="<?php echo htmlspecialchars($student['student_id']); ?>"
<?php echo ($internship_data && $internship_data['student_id'] == $student['student_id']) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($student['student_id'] . ' - ' . $student['first_name'] . ' ' . $student['last_name']); ?>
</option>
<?php endwhile; ?>
</select>
</div>
<div class="gc-6">
<label class="form-label" for="company_id">สถานประกอบการ<span class="required">*</span></label>
<select id="company_id" name="company_id" class="input" required>
<option value="">เลือกสถานประกอบการ</option>
<?php while($company = mysqli_fetch_assoc($companies_result)): ?>
<option value="<?php echo $company['company_id']; ?>"
<?php echo ($internship_data && $internship_data['company_id'] == $company['company_id']) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($company['company_name'] . ' (' . $company['province'] . ')'); ?>
</option>
<?php endwhile; ?>
</select>
</div>
<div class="gc-4">
<label class="form-label" for="start_date">วันที่เริ่มฝึกงาน<span class="required">*</span></label>
<input type="date" id="start_date" name="start_date" class="input" required
value="<?php echo $internship_data ? htmlspecialchars($internship_data['start_date']) : ''; ?>">
</div>
<div class="gc-4">
<label class="form-label" for="end_date">วันที่สิ้นสุด<span class="required">*</span></label>
<input type="date" id="end_date" name="end_date" class="input" required
value="<?php echo $internship_data ? htmlspecialchars($internship_data['end_date']) : ''; ?>">
<span class="form-help">ต้องไม่มาก่อนวันที่เริ่มฝึกงาน</span>
</div>
<div class="gc-4">
<label class="form-label" for="status">สถานะ<span class="required">*</span></label>
<select id="status" name="status" class="input" required>
<option value="">เลือกสถานะ</option>
<?php foreach($statuses as $status): ?>
<option value="<?php echo $status; ?>"
<?php echo ($internship_data && $internship_data['status'] == $status) ? 'selected' : ''; ?>>
<?php echo $status; ?>
</option>
<?php endforeach; ?>
</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="internships.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"); ?>