<?php

require_once "config.php";

// Set header to specify content type as JSON
header('Content-Type: application/json');

// Get the HTTP method used
$method = $_SERVER['REQUEST_METHOD'];

// Perform actions based on the HTTP method
switch ($method) {
    case 'GET':
        // Get the value of the "cNama" parameter from the query string
        $cNama = $_GET["cNama"];

        // Perform SQL injection prevention by using prepared statements
        $query = $conn->prepare("SELECT * FROM tbbarang WHERE cNama LIKE ?");
        $searchTerm = "%$cNama%"; // Append wildcard to search term
        $query->bind_param("s", $searchTerm); // Bind the parameter
        $query->execute(); // Execute the prepared statement
        $result = $query->get_result(); // Get the result set
        $data = $result->fetch_all(MYSQLI_ASSOC); // Fetch all rows of the query result as an associative array
        echo json_encode($data);
        break;
}

// Close the database connection
mysqli_close($conn);
?>
