diff --git a/application/Controller/SongsController.php b/application/Controller/SongsController.php index f572577..b791c06 100644 --- a/application/Controller/SongsController.php +++ b/application/Controller/SongsController.php @@ -95,13 +95,16 @@ public function editSong($song_id) // do getSong() in model/model.php $song = $Song->getSong($song_id); - // in a real application we would also check if this db entry exists and therefore show the result or - // redirect the user to an error page or similar - - // load views. within the views we can echo out $song easily - require APP . 'view/_templates/header.php'; - require APP . 'view/songs/edit.php'; - require APP . 'view/_templates/footer.php'; + // If the song wasn't found, then it would have returned false, and we need to display the error page + if ($song === false) { + $page = new \Mini\Controller\ErrorController(); + $page->index(); + } else { + // load views. within the views we can echo out $song easily + require APP . 'view/_templates/header.php'; + require APP . 'view/songs/edit.php'; + require APP . 'view/_templates/footer.php'; + } } else { // redirect user to songs index page (as we don't have a song_id) header('location: ' . URL . 'songs/index'); diff --git a/application/Model/Song.php b/application/Model/Song.php index 1750116..7ff4812 100644 --- a/application/Model/Song.php +++ b/application/Model/Song.php @@ -89,7 +89,7 @@ public function getSong($song_id) $query->execute($parameters); // fetch() is the PDO method that get exactly one result - return $query->fetch(); + return ($query->rowcount() ? $query->fetch() : false); } /**