<?php require_once('header.php'); ?>

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
// try{
//     $pdo = new PDO("mysql:host=localhost;dbname=demo", "root", "");
//     // Set the PDO error mode to exception
//     $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// } catch(PDOException $e){
//     die("ERROR: Could not connect. " . $e->getMessage());
// }
 
// Attempt search query execution

// if(isset($_POST["name"])) {
//     $pincode = $_POST["name"];
//     $sql = "SELECT user_name FROM pincode WHERE pin_code='$pincode'";
//     $stmt = $pdo->prepare($sql);
//         // $term = $_REQUEST["term"] . '%';
//         // bind parameters to statement
//         // $stmt->bindParam(":term", $term);
//         // execute the prepared statement
//         $stmt->execute();
//         if($stmt->rowCount() > 0){
//             echo ' Pincode already available';
//         }
// }

try{
    if(isset($_REQUEST["term"])){
        // create prepared statement
        $sql = "SELECT * FROM pincode WHERE pin_code LIKE :term";
        $stmt = $pdo->prepare($sql);
        $term = $_REQUEST["term"] . '%';
        // bind parameters to statement
        $stmt->bindParam(":term", $term);
        // execute the prepared statement
        $stmt->execute();
        if($stmt->rowCount() > 0){
            while($row = $stmt->fetch()){
                echo "<table><tr><td>" . $row["pin_code"] . "<td>";
                echo "<td>" . $row["city"] . "<td>";
                echo "<td>" . $row["state"] . "<td><tr><table>";
            }
        } else{
            echo "<p>No matches found</p>";
        }
    }  
} catch(PDOException $e){
    die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}
 
// Close statement
unset($stmt);
 
// Close connection
unset($pdo);
?>