| 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 : /laragon2/www/bpkd/api/ |
Upload File : |
<?php
// SET HEADER
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: access");
header("Access-Control-Allow-Methods: PUT");
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
$get_post = "SELECT * FROM `posts` WHERE id=:post_id";
$get_stmt = $conn->prepare($get_post);
$get_stmt->bindValue(':post_id', $post_id,PDO::PARAM_INT);
$get_stmt->execute();
//CHECK WHETHER THERE IS ANY POST IN OUR DATABASE
if($get_stmt->rowCount() > 0){
// FETCH POST FROM DATBASE
$row = $get_stmt->fetch(PDO::FETCH_ASSOC);
// CHECK, IF NEW UPDATE REQUEST DATA IS AVAILABLE THEN SET IT OTHERWISE SET OLD DATA
$post_title = isset($data->title) ? $data->title : $row['title'];
$post_body = isset($data->body) ? $data->body : $row['body'];
$post_author = isset($data->author) ? $data->author : $row['author'];
$update_query = "UPDATE `posts` SET title = :title, body = :body, author = :author
WHERE id = :id";
$update_stmt = $conn->prepare($update_query);
// DATA BINDING AND REMOVE SPECIAL CHARS AND REMOVE TAGS
$update_stmt->bindValue(':title', htmlspecialchars(strip_tags($post_title)),PDO::PARAM_STR);
$update_stmt->bindValue(':body', htmlspecialchars(strip_tags($post_body)),PDO::PARAM_STR);
$update_stmt->bindValue(':author', htmlspecialchars(strip_tags($post_author)),PDO::PARAM_STR);
$update_stmt->bindValue(':id', $post_id,PDO::PARAM_INT);
if($update_stmt->execute()){
$msg['message'] = 'Data updated successfully';
}else{
$msg['message'] = 'data not updated';
}
}
else{
$msg['message'] = 'Invlid ID';
}
echo json_encode($msg);
}
?>