prepare($sql); $stmt->execute([ ':name' => $name, ':email' => $email, ':phone' => $phone !== '' ? $phone : null, ':birthday' => $birthday !== '' ? $birthday : null, ]); $msg = "Đã thêm sinh viên \"{$name}\" bằng Prepared Statement!"; $executedQueryLog = $sql; } } elseif ($action === 'update') { $id = (int)($_POST['id'] ?? 0); $name = trim((string)($_POST['name'] ?? '')); $email = trim((string)($_POST['email'] ?? '')); $phone = trim((string)($_POST['phone'] ?? '')); $birthday = trim((string)($_POST['birthday'] ?? '')); if ($id <= 0 || $name === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) { $err = 'Thông tin cập nhật không hợp lệ.'; } else { $sql = "UPDATE students SET name = :name, email = :email, phone = :phone, birthday = :birthday WHERE id = :id"; $stmt = $pdo->prepare($sql); $stmt->execute([ ':name' => $name, ':email' => $email, ':phone' => $phone !== '' ? $phone : null, ':birthday' => $birthday !== '' ? $birthday : null, ':id' => $id, ]); $msg = "Đã cập nhật sinh viên #{$id} ({$name}) bằng Prepared Statement!"; $executedQueryLog = $sql; } } elseif ($action === 'delete') { $id = (int)($_POST['id'] ?? 0); if ($id > 0) { $sql = "DELETE FROM students WHERE id = :id"; $stmt = $pdo->prepare($sql); $stmt->execute([':id' => $id]); $msg = "Đã xóa sinh viên #{$id} bằng Prepared Statement!"; $executedQueryLog = $sql; } } } // Nếu người dùng chọn Sửa 1 sinh viên -> dùng Prepared Statement lấy chi tiết if (isset($_GET['edit_id'])) { $editStmt = $pdo->prepare("SELECT id, name, email, phone, birthday FROM students WHERE id = :id"); $editStmt->execute([':id' => (int)$_GET['edit_id']]); $editingStudent = $editStmt->fetch() ?: null; } // Truy vấn danh sách hoặc tìm kiếm đều dùng prepare() + execute() if ($keyword !== '') { $listSql = "SELECT id, name, email, phone, birthday, created_at FROM students WHERE name LIKE :kw1 OR email LIKE :kw2 ORDER BY id DESC"; $listStmt = $pdo->prepare($listSql); $listStmt->execute([ ':kw1' => '%' . $keyword . '%', ':kw2' => '%' . $keyword . '%', ]); if ($executedQueryLog === '') { $executedQueryLog = $listSql; } } else { $listSql = "SELECT id, name, email, phone, birthday, created_at FROM students WHERE id >= :min_id ORDER BY id DESC"; $listStmt = $pdo->prepare($listSql); $listStmt->execute([':min_id' => 1]); if ($executedQueryLog === '') { $executedQueryLog = $listSql; } } $students = $listStmt->fetchAll(); } catch (Throwable $e) { $connectionError = $e; } ?>
= htmlspecialchars($executedQueryLog) ?>
$stmt = $pdo->prepare($sql);
$stmt->execute([
':name' => $name,
':email' => $email,
':phone' => $phone,
':birthday' => $birthday
]);
prepare() & execute()
| ID | Họ và tên | Điện thoại | Ngày sinh | Thao tác | |
|---|---|---|---|---|---|
| Không có bản ghi nào phù hợp. | |||||
| #= (int)$sv['id'] ?> | = htmlspecialchars((string)$sv['name']) ?> | = htmlspecialchars((string)$sv['email']) ?> | = htmlspecialchars((string)($sv['phone'] ?? '—')) ?> | = !empty($sv['birthday']) ? date('d/m/Y', strtotime((string)$sv['birthday'])) : '—' ?> | Sửa |