| Server IP : 127.0.0.1 / Your IP : 192.168.99.1 Web Server : Apache/2.4.35 (Win64) OpenSSL/1.1.1b mod_fcgid/2.3.9 System : Windows NT WIN-JEJMB9EN0L0 10.0 build 14393 (Windows Server 2016) AMD64 User : Administrator ( 0) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : D:/laragon/www/bpkd/api/ |
Upload File : |
<?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);
}
?>