prepare("SELECT * FROM papers WHERE id = ?"); $stmt->execute([$paper_id]); $paper = $stmt->fetch(); if (!$paper) { header("Location: index.php"); exit; } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $title = trim($_POST['title'] ?? ''); $authors = trim($_POST['authors'] ?? ''); $publication = trim($_POST['publication'] ?? ''); $year = filter_input(INPUT_POST, 'year', FILTER_VALIDATE_INT, ['options' => ['min_range' => 1900, 'max_range' => date('Y') + 1]]); $notes = trim($_POST['notes'] ?? ''); if (empty($title) || empty($authors)) { $feedback = ['type' => 'danger', 'message' => 'Title and Authors are required.']; } elseif ($year === false) { $feedback = ['type' => 'danger', 'message' => 'Invalid year.']; } else { try { $stmt = $pdo->prepare("UPDATE papers SET title = ?, authors = ?, publication = ?, year = ?, notes = ? WHERE id = ?"); $stmt->execute([$title, $authors, $publication, $year, $notes, $paper_id]); header("Location: index.php?success=updated"); exit; } catch (PDOException $e) { $feedback = ['type' => 'danger', 'message' => 'Error updating paper: ' . $e->getMessage()]; } } // To show feedback on the same page, we need to repopulate the paper variable with submitted data $paper['title'] = $title; $paper['authors'] = $authors; $paper['publication'] = $publication; $paper['year'] = $year; $paper['notes'] = $notes; } ?>