<?php
// Include DB file for credentials and database connection 


include("db.php");

error_reporting(E_ALL);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	
	
	
	$data = json_decode(file_get_contents('php://input'), true);
	
	$return = array('success' => false, 'message' => '');
	
	
	$stmt = $conn->prepare("SELECT * FROM tbl_countries WHERE name = ? LIMIT 1");
	$stmt->bind_param("s", $data['cid']);
    $stmt->execute();
    $resultg = $stmt->get_result();
	$rowg = $resultg->fetch_assoc();
	
	
	if($resultg->num_rows > 0) {
	
		$stmt = $conn->prepare("SELECT * FROM tbl_states WHERE country_id = ? order by name");
		$stmt->bind_param("s", $rowg['id']);
		$stmt->execute();
		$result = $stmt->get_result();
		
		$rows  = [];
		
		if($result->num_rows > 0) {
		
			while($row = $result->fetch_assoc()) {
				
				$rows[] = $row;	
			}
			$return = array('success' => true, 'message' => 'Successfully fetched data!', 'data' => $rows);
			
		} else {
			
			$return = array('success' => false, 'message' => 'No record found!..', 'data' => $rows);
			
		}
	
	}

	$stmt->close();
	$conn->close();
		
	echo json_encode($return);
	 
}  



