prepare(' SELECT c.id AS id, c.author_id AS author_id, c.isFavorite AS isFavorite, c.isArchived AS isArchived, c.adjective_id AS adjective_id, c.noun_id AS noun_id, c.verb_id AS verb_id, c.location_id AS location_id, a.value AS adjective, n.value AS noun, v.value AS verb, l.value AS location, c.image AS image FROM critters c JOIN adjectives a ON a.id = c.adjective_id JOIN nouns n ON n.id = c.noun_id JOIN verbs v ON v.id = c.verb_id JOIN locations l ON l.id = c.location_id WHERE c.id = :id AND c.author_id = :author_id '); $stmt->execute(['id' => $critterId, 'author_id' => $userId]); return $stmt->fetch(PDO::FETCH_ASSOC); } $critter = null; if ($critterId !== null && $critterId !== false) { $critter = fetchOwnedCritter($pdo, (int) $_SESSION['user_id'], $critterId); } $isOwner = (bool) $critter; $relatedCritters = []; if ($critter) { $relatedStmt = $pdo->prepare(' SELECT id, image FROM critters WHERE author_id = :author_id AND adjective_id = :adjective_id AND noun_id = :noun_id AND verb_id = :verb_id AND location_id = :location_id AND id != :current_id ORDER BY id DESC '); $relatedStmt->execute([ 'author_id' => $critter['author_id'], 'adjective_id' => $critter['adjective_id'], 'noun_id' => $critter['noun_id'], 'verb_id' => $critter['verb_id'], 'location_id' => $critter['location_id'], 'current_id' => $critter['id'], ]); $relatedCritters = array_values(array_filter( $relatedStmt->fetchAll(PDO::FETCH_ASSOC), fn($row) => !empty($row['image']) )); } if ($_SERVER['REQUEST_METHOD'] === 'POST' && $critter && $isOwner && csrf_verify($_POST['csrf_token'] ?? null)) { $action = $_POST['action'] ?? ''; if ($action === 'toggle_favorite') { $newValue = $critter['isFavorite'] ? 0 : 1; $stmt = $pdo->prepare('UPDATE critters SET isFavorite = :value WHERE id = :id'); $stmt->execute(['value' => $newValue, 'id' => $critterId]); } elseif ($action === 'toggle_archived') { $newValue = $critter['isArchived'] ? 0 : 1; $stmt = $pdo->prepare('UPDATE critters SET isArchived = :value WHERE id = :id'); $stmt->execute(['value' => $newValue, 'id' => $critterId]); } header('Location: single-critter.php?id=' . $critterId); exit; } $pageTitle = 'View Critter'; ?>
The
Critter not found.