First you need to create database with three table as country, state and city. state table has relation with country table and city table has relation with state table.
Country State City tabel
CREATE TABLE `country` (
`country_id` INT( 3 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`country_name` VARCHAR( 25 ) NOT NULL
) ENGINE = MYISAM ;
CREATE TABLE `state` (
`state_id` INT( 3 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`country_id` INT( 3 ) NOT NULL ,
`state_name` VARCHAR( 35 ) NOT NULL
) ENGINE = MYISAM ;
CREATE TABLE `city` (
`city_id` INT( 3 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`state_id` INT( 3 ) NOT NULL ,
`city_name` VARCHAR( 35 ) NOT NULL
) ENGINE = MYISAM ;
Above are the three table the we use in this tutorials each table have three columns which are dependent to each other.
Dbconfig.Php
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
$e->getMessage();
}
Index.Php
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
$e->getMessage();
}
Country : --Select Country-- prepare("SELECT * FROM country"); $stmt->execute(); while($row=$stmt->fetch(PDO::FETCH_ASSOC)) { ?>
get_state.php
prepare("SELECT * FROM state WHERE country_id=:id");
$stmt->execute(array(':id' => $id));
?>
Select State :fetch(PDO::FETCH_ASSOC)) { ?>
get_city.php
prepare("SELECT * FROM city WHERE state_id=:id");
$stmt->execute(array(':id' => $id));
?>
Select City : fetch(PDO::FETCH_ASSOC)) { ?>