tech4biz/admin/api/add_blog_data.php
2024-10-24 17:20:08 +05:30

61 lines
2.7 KiB
PHP

<?php
require_once "../db/config.php";
require_once "../includes/functions.php";
$_REQUEST_METHOD = $_SERVER['REQUEST_METHOD'];
$response = array();
if ($_REQUEST_METHOD === 'POST') {
$blog_ref_id = $_POST['blog_ref_id'];
$api_type = $_POST['api_type'];
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < 20; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
if (isset($connection)) {
$api_type = $_POST['api_type'];
if($api_type=='delete'){
// $id = $_POST['id'];
$updateCategory = $connection->query("DELETE FROM `blogs_data` WHERE blog_ref_id='$blog_ref_id'");
}else{
// $blog_header_image = $_POST['blog_header_image'];
$category_id = $_POST['category_id'];
$category_sub_id = $_POST['category_sub_id'];
$blog_read_time = $_POST['blog_read_time'];
$blog_title = $_POST['blog_title'];
$developer_id = $_POST['developer_id'];
$blog_details_content = $_POST['blog_details_content'];
if (is_uploaded_file($_FILES['blog_header_image']['tmp_name'])) {
$tmp_file1 = $_FILES['blog_header_image']['tmp_name'];
$post_name = $_FILES['blog_header_image']['name'];
$extension1 = explode(".", $post_name);
$unix_ms_time1 = intval(microtime(true) * 1000);
$inputed_post = $unix_ms_time1 . '.' . end($extension1);
$upload_dir1 = "../blog_image/" . $inputed_post;
move_uploaded_file($tmp_file1, $upload_dir1);
}
if(!isset($_POST['id'])){
$insertCategory = $connection->query("INSERT INTO `blogs_data`(`category_id`, `category_sub_id`, `blog_read_time`, `blog_title`, `developer_id`, `blog_details_content`, `blog_ref_id`) VALUES ($category_id, $category_sub_id, '$blog_read_time', '$blog_title', $developer_id, '$blog_details_content', '$randomString')");
}else{
$id = $_POST['id'];
$updateCategory = $connection->query("UPDATE `blogs_data` SET `category_id`='$category_id',`category_sub_id`='$category_sub_id',`blog_read_time`='$blog_read_time',`blog_title`='$blog_title',`developer_id`='$developer_id',`blog_details_content`='$blog_details_content',`blog_header_image`='$blog_header_image' WHERE `blog_ref_id`='$blog_ref_id'");
}
}
$response["message"] = "success";
$response["status"] = 200;
}
} else {
$response["message"] = "INVALID REQUEST";
$response["status"] = 400;
}
echo json_encode($response);