<?php
header("Content-Type: application/json");
// SECURITY TOKEN
$API_TOKEN = "123456";
// Validate Token
if (!isset($_GET['token']) || $_GET['token'] != $API_TOKEN) {
echo json_encode(["status" => "error", "msg" => "Invalid Token"]);
exit;
}
// MySQL Connection
$host = "localhost";
$user = "amarnat2_Chairman";
$pass = "Chairman&&0815";
$db = "amarnat2_chairmandb";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
echo json_encode(["status" => "error", "msg" => "DB Connection Failed"]);
exit;
}
$action = $_GET['action'];
// ------------------------------
// INSERT
// ------------------------------
if ($action == "insert") {
$name = $_GET['name'];
$mobile = $_GET['mobile'];
$amount = $_GET['amount'];
$stmt = $conn->prepare("INSERT INTO customers (name, mobile, amount) VALUES (?,?,?)");
$stmt->bind_param("ssd", $name, $mobile, $amount);
if ($stmt->execute()) {
echo json_encode(["status" => "success", "msg" => "Inserted"]);
} else {
echo json_encode(["status" => "error", "msg" => "Insert Failed"]);
}
}
// ------------------------------
// UPDATE
// ------------------------------
if ($action == "update") {
$id = $_GET['id'];
$name = $_GET['name'];
$mobile = $_GET['mobile'];
$amount = $_GET['amount'];
$stmt = $conn->prepare("UPDATE customers SET name=?, mobile=?, amount=? WHERE id=?");
$stmt->bind_param("ssdi", $name, $mobile, $amount, $id);
if ($stmt->execute()) {
echo json_encode(["status" => "success", "msg" => "Updated"]);
} else {
echo json_encode(["status" => "error", "msg" => "Update Failed"]);
}
}
// ------------------------------
// DELETE
// ------------------------------
if ($action == "delete") {
$id = $_GET['id'];
$stmt = $conn->prepare("DELETE FROM customers WHERE id=?");
$stmt->bind_param("i", $id);
if ($stmt->execute()) {
echo json_encode(["status" => "success", "msg" => "Deleted"]);
} else {
echo json_encode(["status" => "error", "msg" => "Delete Failed"]);
}
}
// ------------------------------
// SELECT ALL
// ------------------------------
if ($action == "select") {
$result = $conn->query("SELECT * FROM customers ORDER BY id DESC");
$rows = [];
while ($r = $result->fetch_assoc()) {
$rows[] = $r;
}
echo json_encode(["status" => "success", "data" => $rows]);
}
$conn->close();
?>