34762-vm/edit_paper.php
Flatlogic Bot 5235a9fa88 v1
2025-10-07 17:01:46 +00:00

100 lines
4.3 KiB
PHP

<?php
require_once 'db/config.php';
include 'partials/header.php';
$pdo = db();
$feedback = [];
$paper = null;
if (!isset($_GET['id']) || !filter_var($_GET['id'], FILTER_VALIDATE_INT)) {
header("Location: index.php");
exit;
}
$paper_id = $_GET['id'];
// Fetch paper details
$stmt = $pdo->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;
}
?>
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<h2>Edit Paper</h2>
</div>
<div class="card-body">
<?php if (!empty($feedback)): ?>
<div class="alert alert-<?= $feedback['type'] ?>">
<?= htmlspecialchars($feedback['message']) ?>
</div>
<?php endif; ?>
<form action="edit_paper.php?id=<?= $paper_id ?>" method="post">
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" name="title" value="<?= htmlspecialchars($paper['title']) ?>" required>
</div>
<div class="mb-3">
<label for="authors" class="form-label">Authors</label>
<input type="text" class="form-control" id="authors" name="authors" value="<?= htmlspecialchars($paper['authors']) ?>" required>
</div>
<div class="mb-3">
<label for="publication" class="form-label">Publication/Journal</label>
<input type="text" class="form-control" id="publication" name="publication" value="<?= htmlspecialchars($paper['publication']) ?>">
</div>
<div class="mb-3">
<label for="year" class="form-label">Year</label>
<input type="number" class="form-control" id="year" name="year" value="<?= htmlspecialchars($paper['year']) ?>" min="1900" max="<?= date('Y') + 1 ?>">
</div>
<div class="mb-3">
<label for="notes" class="form-label">Notes</label>
<textarea class="form-control" id="notes" name="notes" rows="5"><?= htmlspecialchars($paper['notes']) ?></textarea>
</div>
<button type="submit" class="btn btn-primary">Update Paper</button>
<a href="index.php" class="btn btn-secondary">Cancel</a>
</form>
</div>
</div>
</div>
</div>
</div>
<?php include 'partials/footer.php'; ?>