I have created this php service to retrieve data from the database "and display results from tbl_halls but I'm getting warnings only. Please take a look at the code below.
<?php
//fetch_data.php
$connect = new PDO("mysql:host=localhost;dbname=kawadb", "root", "");
$method = $_SERVER['REQUEST_METHOD'];
if($method == 'GET'
//&& isset($_GET['hall_id']) && isset($_GET['hall_name']) && isset($_GET['hall_department']) && isset($_GET['hall_floor']) && isset($_GET['capacity'])
)
{
$data = array(
':hall_name' => "%" . $_GET['hall_name'] . "%",
':hall_department' => "%" . $_GET['hall_department'] . "%",
':hall_floor' => "%" . $_GET['hall_floor'] . "%",
':capacity' => "%" . $_GET['capacity'] . "%"
);
{
$query = "SELECT * FROM tbl_halls WHERE hall_name LIKE :hall_name AND hall_department LIKE :hall_department AND hall_floor LIKE :hall_floor AND capacity LIKE :capacity BY hall_id DESC";
$statement = $connect->prepare($query);
$statement->execute($data);
$result = $statement->fetchAll();
foreach($result as $row)
{
$output[] = array(
'hall_id' => $row['hall_id'],
'hall_name' => $row['hall_name'],
'hall_department' => $row['hall_department'],
'hall_floor' => $row['hall_floor'],
'capacity' => $row['capacity']
);
}
header("Content-Type: application/json");
echo json_encode($output);
}
if($method == "POST")
{
$data = array(
':hall_name' => $_POST["hall_name"],
':hall_department' => $_POST["hall_department"],
':hall_floor' => $_POST["hall_floor"],
':capacity' => $_POST["capacity"]
);
$query = "INSERT INTO tbl_halls (hall_name, hall_department, hall_floor,capacity) VALUES (:hall_name, :hall_department, :hall_floor, :capacity)";
$statement = $connect->prepare($query);
$statement->execute($data);
}
if($method == 'PUT')
{
parse_str(file_get_contents("php://input"), $_PUT);
$data = array(
':hall_id' => $_PUT['hall_id'],
':hall_name' => $_PUT['hall_name'],
':hall_department' => $_PUT['hall_department'],
':hall_floor' => $_PUT['hall_floor'],
':capacity' => $_PUT['capacity']
);
$query = "
UPDATE tbl_halls
SET hall_name = :hall_name,
hall_department = :hall_department,
hall_floor = :hall_floor,
capacity = :capacity,
WHERE hall_id = :hall_id
";
$statement = $connect->prepare($query);
$statement->execute($data);
}
if($method == "DELETE")
{
parse_str(file_get_contents("php://input"), $_DELETE);
$query = "DELETE FROM tbl_halls WHERE hall_id = '".$_DELETE["hall_id"]."'";
$statement = $connect->prepare($query);
$statement->execute();
}
}
?>
Kind regards
I have tried
if($method == 'GET' &&
isset($_GET['hall_id']) && isset($_GET['hall_name']) &&
isset($_GET['hall_department']) && isset($_GET['hall_floor']) &&
isset($_GET['capacity'])
But I get nothing at all
I have tried displaying data from the table but the output for every field was Undefined index: hall_name.