From 5ec4888294535777c38ca044c5ac57ddd79b0dc1 Mon Sep 17 00:00:00 2001 From: Mark Hewitt Date: Thu, 29 Mar 2018 09:54:35 -0400 Subject: [PATCH 1/3] Added row count checking on getSong to return false if there are no rows --- application/Model/Song.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); } /** From 766751dc78d3afe4f15ee611fb50b476bc0e32db Mon Sep 17 00:00:00 2001 From: Mark Hewitt Date: Thu, 29 Mar 2018 09:56:25 -0400 Subject: [PATCH 2/3] Load error page if editsong $song_id doesn't exist in db --- application/Controller/SongsController.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/application/Controller/SongsController.php b/application/Controller/SongsController.php index f572577..89bcfe3 100644 --- a/application/Controller/SongsController.php +++ b/application/Controller/SongsController.php @@ -97,11 +97,18 @@ public function editSong($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 (!$song) + { + $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'); From 7faa39e6ee4bc2c7a6f7ac2514472405eccbdb3e Mon Sep 17 00:00:00 2001 From: Mark Hewitt Date: Thu, 29 Mar 2018 13:12:24 -0400 Subject: [PATCH 3/3] Cleaned up the braces, and changed the conditional --- application/Controller/SongsController.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/application/Controller/SongsController.php b/application/Controller/SongsController.php index 89bcfe3..b791c06 100644 --- a/application/Controller/SongsController.php +++ b/application/Controller/SongsController.php @@ -95,15 +95,11 @@ 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 - if (!$song) - { + // 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 - { + } else { // load views. within the views we can echo out $song easily require APP . 'view/_templates/header.php'; require APP . 'view/songs/edit.php';