-
Notifications
You must be signed in to change notification settings - Fork 15
/
delete.php
52 lines (41 loc) · 1.62 KB
/
delete.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
// SET HEADER
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: access");
header("Access-Control-Allow-Methods: DELETE");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// INCLUDING DATABASE AND MAKING OBJECT
require 'database.php';
$db_connection = new Database();
$conn = $db_connection->dbConnection();
// GET DATA FORM REQUEST
$data = json_decode(file_get_contents("php://input"));
//CHECKING, IF ID AVAILABLE ON $data
if(isset($data->id)){
$msg['message'] = '';
$post_id = $data->id;
//GET POST BY ID FROM DATABASE
// YOU CAN REMOVE THIS QUERY AND PERFORM ONLY DELETE QUERY
$check_post = "SELECT * FROM `posts` WHERE id=:post_id";
$check_post_stmt = $conn->prepare($check_post);
$check_post_stmt->bindValue(':post_id', $post_id,PDO::PARAM_INT);
$check_post_stmt->execute();
//CHECK WHETHER THERE IS ANY POST IN OUR DATABASE
if($check_post_stmt->rowCount() > 0){
//DELETE POST BY ID FROM DATABASE
$delete_post = "DELETE FROM `posts` WHERE id=:post_id";
$delete_post_stmt = $conn->prepare($delete_post);
$delete_post_stmt->bindValue(':post_id', $post_id,PDO::PARAM_INT);
if($delete_post_stmt->execute()){
$msg['message'] = 'Post Deleted Successfully';
}else{
$msg['message'] = 'Post Not Deleted';
}
}else{
$msg['message'] = 'Invlid ID';
}
// ECHO MESSAGE IN JSON FORMAT
echo json_encode($msg);
}
?>