TOOGLE
Php CRUD Basic BackEnd - Back-End
<?php
    header('Content-Type: application/json');
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, OPTIONS, DELETE, PUT');
    header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');

    $servername = "";
    $username = "";
    $password = "";
    $dbname = "";

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

    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
        http_response_code(200);
        exit();
    }
    if ($conn->connect_error) {
        die(json_encode(array("error" => "Connection failed: " . $conn->connect_error)));
    }

    if ($_SERVER['REQUEST_METHOD'] === 'POST') {

        $data = json_decode(file_get_contents('php://input'), true);
        $id = $data['id'];
        $info_first = $data['info_first'];
        $info_second = $data['info_second'];

        if (!empty($info_first) && !empty($info_second)) {

            if(isset($data['id']) && isset($data['action']) && $data['action'] === 'update') {
                $id = $data['id'];
                $stmt = $conn->prepare("UPDATE table SET info_first = ?, info_second = ? WHERE id = ?");
                $stmt->bind_param("ssi", $info_first, $info_second, $id);
                if ($stmt->execute()) {
                    echo json_encode(array("message" => "Trade updated successfully"));
                } else {
                    echo json_encode(array("error" => "Error updating trade: " . $stmt->error));
                }
                $stmt->close();
            }

            else{
                $stmt = $conn->prepare("INSERT INTO table (info_first, info_second) VALUES (?, ?)");
                $stmt->bind_param("ss", $info_first, $info_second);
                if ($stmt->execute()) {
                    echo json_encode(array("message" => "Trade added successfully"));
                } else {
                    echo json_encode(array("error" => "Error adding trade: " . $stmt->error));
                }
                $stmt->close();
            }

        }else {
            echo json_encode(array("error" => "Request not successful"));
        }
    } 

    else if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
        $data = json_decode(file_get_contents('php://input'), true);

        if (isset($_GET['deleteId']) && isset($data['password'])) {
            $id = $_GET['deleteId'];
            $password = $data['password'];

            if ($password === '987654321') { 
                $stmt = $conn->prepare("DELETE FROM table WHERE id = ?");
                $stmt->bind_param("i", $id);

                if ($stmt->execute()) {
                    echo json_encode(array("message" => "Trade deleted successfully"));
                } else {
                    echo json_encode(array("error" => "Error deleting trade"));
                }
            } else {
                echo json_encode(array("error" => "Incorrect password"));
            }
        }else {
            echo json_encode(array("error" => "ID and password are required for deletion"));
        }
    }
    else if ($_SERVER['REQUEST_METHOD'] === 'GET') {

        if(isset($_GET['info_first'])) {
            $info_first = $_GET['info_first'];
            $sql = "SELECT id, info_first, info_second FROM table WHERE info_first = ?";
            $stmt = $conn->prepare($sql);
            $stmt->bind_param("s", $info_first);
            $stmt->execute();
            $result = $stmt->get_result();
            $table_data = array();

            if ($result->num_rows > 0) {
                while ($row = $result->fetch_assoc()) {
                    $table_data[] = $row;
                }
            } else {
                echo json_encode(array("message" => "0 results"));
                exit();
            }
            echo json_encode($table_data);
        }
      
        else if(isset($_GET['getById'])) {
            $id = $_GET['getById'];
            $sql = "SELECT id, info_first, info_second FROM table WHERE id = ?";
            $stmt = $conn->prepare($sql);
            $stmt->bind_param("i", $id);
            $stmt->execute();
            $result = $stmt->get_result();
            $table_data = array();

            if ($result->num_rows > 0) {
                while ($row = $result->fetch_assoc()) {
                    $table_data[] = $row;
                }
            } else {
                echo json_encode(array("message" => "0 results"));
                exit();
            }
            echo json_encode($table_data);
        }
        else {
            $sql = "SELECT id, info_first, info_second FROM table";
            $result = $conn->query($sql);
            $table_data = array();

            if ($result->num_rows > 0) {
                while ($row = $result->fetch_assoc()) {
                    $table_data[] = $row;
                }
            } else {
                echo json_encode(array("message" => "0 results"));
                exit();
            }
            echo json_encode($table_data);
        }
    }

    $conn->close();
?>