prepare("INSERT INTO competitions (title, details, rank_result, year_th) VALUES (?, ?, ?, ?)"); $stmt->execute([$title, $details, $rank_result, $year_th]); $competition_id = $pdo->lastInsertId(); // Handle authors $authors = array_filter(array_map('trim', explode(',', $authors_str))); foreach ($authors as $author) { $stmt = $pdo->prepare("INSERT INTO competition_authors (competition_id, author_name) VALUES (?, ?)"); $stmt->execute([$competition_id, $author]); } // Handle file uploads if (!empty($_FILES['attachments']['name'][0])) { foreach ($_FILES['attachments']['name'] as $key => $name) { if ($_FILES['attachments']['error'][$key] == 0) { $tmp_name = $_FILES['attachments']['tmp_name'][$key]; $gdrive_file_id = uploadFileToDrive($tmp_name, $name, $folder_id); if ($gdrive_file_id) { $stmt = $pdo->prepare("INSERT INTO competition_attachments (competition_id, file_name, gdrive_file_id) VALUES (?, ?, ?)"); $stmt->execute([$competition_id, $name, $gdrive_file_id]); } } } } logActivity($pdo, 'CREATE_COMPETITION', "เพิ่มผลงาน: $title"); header("Location: competitions.php"); exit; } if (isset($_POST['edit_competition'])) { $id = $_POST['id']; $title = $_POST['title']; $year_th = $_POST['year_th']; $rank_result = $_POST['rank_result']; $details = $_POST['details']; $authors_str = $_POST['authors']; $stmt = $pdo->prepare("UPDATE competitions SET title = ?, details = ?, rank_result = ?, year_th = ? WHERE id = ?"); $stmt->execute([$title, $details, $rank_result, $year_th, $id]); // Update authors (delete all and re-insert) $stmt = $pdo->prepare("DELETE FROM competition_authors WHERE competition_id = ?"); $stmt->execute([$id]); $authors = array_filter(array_map('trim', explode(',', $authors_str))); foreach ($authors as $author) { $stmt = $pdo->prepare("INSERT INTO competition_authors (competition_id, author_name) VALUES (?, ?)"); $stmt->execute([$id, $author]); } // Handle new file uploads if (!empty($_FILES['attachments']['name'][0])) { foreach ($_FILES['attachments']['name'] as $key => $name) { if ($_FILES['attachments']['error'][$key] == 0) { $tmp_name = $_FILES['attachments']['tmp_name'][$key]; $gdrive_file_id = uploadFileToDrive($tmp_name, $name, $folder_id); if ($gdrive_file_id) { $stmt = $pdo->prepare("INSERT INTO competition_attachments (competition_id, file_name, gdrive_file_id) VALUES (?, ?, ?)"); $stmt->execute([$id, $name, $gdrive_file_id]); } } } } logActivity($pdo, 'UPDATE_COMPETITION', "แก้ไขผลงาน: $title"); header("Location: competitions.php"); exit; } } if ($action === 'delete_attachment' && isset($_GET['id'])) { $id = $_GET['id']; $stmt = $pdo->prepare("SELECT * FROM competition_attachments WHERE id = ?"); $stmt->execute([$id]); $attachment = $stmt->fetch(); if ($attachment) { deleteFileFromDrive($attachment['gdrive_file_id']); $stmt = $pdo->prepare("DELETE FROM competition_attachments WHERE id = ?"); $stmt->execute([$id]); logActivity($pdo, 'DELETE_COMPETITION_FILE', "ลบไฟล์แนบผลงาน: " . $attachment['file_name']); } header("Location: competitions.php?action=edit&id=" . $attachment['competition_id']); exit; } if ($action === 'delete' && isset($_GET['id'])) { $id = $_GET['id']; $stmt = $pdo->prepare("SELECT title FROM competitions WHERE id = ?"); $stmt->execute([$id]); $comp = $stmt->fetch(); if ($comp) { // Delete all attachments from GDrive $stmt = $pdo->prepare("SELECT * FROM competition_attachments WHERE competition_id = ?"); $stmt->execute([$id]); $attachments = $stmt->fetchAll(); foreach ($attachments as $attachment) { deleteFileFromDrive($attachment['gdrive_file_id']); } $stmt = $pdo->prepare("DELETE FROM competitions WHERE id = ?"); $stmt->execute([$id]); logActivity($pdo, 'DELETE_COMPETITION', "ลบผลงาน: " . $comp['title']); } header("Location: competitions.php"); exit; } ?>
| ปี (พ.ศ.) | ชื่อผลงาน | รางวัล | ผู้จัดทำ | ไฟล์แนบ | จัดการ |
|---|---|---|---|---|---|
| = $row['year_th'] ?> | = htmlspecialchars((string)$row['title']) ?> | = htmlspecialchars((string)$row['rank_result']) ?> | = htmlspecialchars((string)$row['authors']) ?> | = $row['file_count'] ?> | แก้ไข ลบ |