Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Check Function to Post.php to check if ID exists in Database or not. #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions api/post/delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
));
}
?>
33 changes: 25 additions & 8 deletions api/post/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
));
}

?>
16 changes: 16 additions & 0 deletions models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down