From 80c54520a8811e5b1eb491f7fd109455bcd2efdd Mon Sep 17 00:00:00 2001 From: Ronak Date: Fri, 30 Aug 2019 00:02:35 +0530 Subject: [PATCH] Added Check Function to Post.php. --- api/post/delete.php | 34 +++++++++++++++++++++++++--------- api/post/update.php | 33 +++++++++++++++++++++++++-------- models/Post.php | 16 ++++++++++++++++ 3 files changed, 66 insertions(+), 17 deletions(-) diff --git a/api/post/delete.php b/api/post/delete.php index 3edb718..7fdcb18 100644 --- a/api/post/delete.php +++ b/api/post/delete.php @@ -22,13 +22,29 @@ $post->id = $data->id; // Delete post - if($post->delete()) { - echo json_encode( - array('message' => 'Post Deleted') - ); - } else { - echo json_encode( - array('message' => 'Post Not Deleted') - ); - } + // Old Code without the Check Function + // if($post->delete()) { + // echo json_encode( + // array('message' => 'Post Deleted') + // ); + // } else { + // echo json_encode( + // array('message' => 'Post Not Deleted') + // ); + // } + + //New Code with Check Functionality + if($post->check()){ + if($post->delete()){ + echo(json_encode( + array('message' => 'Post Deleted') + )); + } + } + else { + echo(json_encode( + array('message' => 'Post Not Deleted') + )); + } +?> \ No newline at end of file diff --git a/api/post/update.php b/api/post/update.php index c8143a1..9267017 100644 --- a/api/post/update.php +++ b/api/post/update.php @@ -27,13 +27,30 @@ $post->category_id = $data->category_id; // Update post - if($post->update()) { - echo json_encode( - array('message' => 'Post Updated') - ); - } else { - echo json_encode( - array('message' => 'Post Not Updated') - ); + + // Old Code without the Check Function + // if($post->update()) { + // echo json_encode( + // array('message' => 'Post Updated') + // ); + // } else { + // echo json_encode( + // array('message' => 'Post Not Updated') + // ); + // } + + //New Code with Check Functionality + if($post->check()){ + if($post->update()){ + echo(json_encode( + array('message' => 'Post Updated') + )); + } + } + else { + echo(json_encode( + array('message' => 'Post Not Updated') + )); } +?> \ No newline at end of file diff --git a/models/Post.php b/models/Post.php index 3205477..db58cc0 100644 --- a/models/Post.php +++ b/models/Post.php @@ -98,6 +98,22 @@ public function create() { return false; } + //Check (Checking Function) + public function check() { + $query = 'SELECT * FROM '.$this->table.' where id = :id'; + + $stmt = $this->conn->prepare($query); + $this->id = htmlspecialchars(strip_tags($this->id)); + $stmt->bindParam(':id', $this->id); + $stmt->execute(); + + if(($stmt->rowCount())>0){ + return true; + } + printf("Error: Please Check Your ID & Try Again.\n"); + return false; + } + // Update Post public function update() { // Create query