<?php
header('Content-Type: application/json; charset=utf-8');

// Database connection details
$servername = "localhost";
$username = "venmobdc_Auto_DataBase_User";
$password = "hB_nf_W-^SIh";
$dbname = "venmobdc_Auto_Database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die(json_encode(array('status' => 'error', 'message' => 'Connection failed: ' . $conn->connect_error)));
}

// Get `id` and `just_chapter` from POST data
$id = isset($_POST['id']) ? trim($_POST['id']) : '';
$just_chapter = isset($_POST['just_chapter']) ? trim($_POST['just_chapter']) : '';

// Validate inputs
if (empty($id)) {
    echo json_encode(array('status' => 'error', 'message' => 'ID not provided.'));
    exit;
}

if (empty($just_chapter)) {
    echo json_encode(array('status' => 'error', 'message' => 'Chapter not provided.'));
    exit;
}

// Prepare the SQL statement
$sql = "DELETE FROM `$just_chapter` WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $id);

// Execute the query
if ($stmt->execute()) {
    echo json_encode(array('status' => 'success', 'message' => 'Record deleted successfully.'));
} else {
    echo json_encode(array('status' => 'error', 'message' => 'Failed to delete record.'));
}

// Close the statement and connection
$stmt->close();
$conn->close();
?>
