I have to do this exercise before Friday and it does not finish to me to leave, I will explain myself. I have in a database countries, cities and data of those cities, to which I connect well. I need to make two selections in html in a row, one from countries and one from cities. Until you select something from the first, nothing will appear in the second and when you select from it, a table with data from the third table will appear. Well, the first select works, but the second does not and I have already given it many laps, but it does not work out. This is the code that I have made:
<html>
<head>
<title>WORLD</title>
</head>
<body>
<?php
define("SERVIDOR","localhost");
define("USUARIO","root");
define("CLAVE","");
//define("BD","world");
$BD="world";
$db;
try{
if($BD!='')
$db=new PDO("mysql:host=".SERVIDOR.";dbname=".$BD.";charset=utf8",USUARIO,CLAVE,array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES 'utf8'"));
else
$db=new PDO("mysql:host=".SERVIDOR.";charset=utf8",USUARIO,CLAVE,array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES 'utf8'"));
$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
$db->setAttribute(PDO::NULL_TO_STRING,true);
if($BD==''){
$sql=file_get_contents('world.sql');
$this->ejecuta_SQL($sql);
}
}catch(PDOException $e){
die("<p><h3>No se ha podido establecer la conexión.
<p>Compruebe si está activado el servidor de bases de
datos MySQL.</h3></p>\n<p>Error: ".$e->getMessage()."</p>\n");
}
echo "<h1>EJERCICIO CONSULTA DE PAISES POR REGIONES</h1>
<form name='form1' method='post' action=\"index.php\">";
$sql1='SELECT Region FROM country';
$resultado1=$db->query($sql1);
echo "Selecciona Region: <select name='campo_busqueda1'>
<option>Selecciona...</option>";
while(($fila1=$resultado1->fetch(PDO::FETCH_ASSOC))!=NULL){
echo '<option value="'.$fila1["Region"].'">'.$fila1["Region"].'</option>';
};
$region=$_POST["campo_busqueda1"];
echo "</select>";
echo "<input type='submit' value='Enviar'>
<br>";echo $region;
$sql2='SELECT Name FROM country WHERE Region="$region"';
$resultado2=$db->query($sql2);
echo "Selecciona Pais: <select name='campo_busqueda2'>
<option>Selecciona...</option>";
while(($fila2=$resultado2->fetch(PDO::FETCH_ASSOC))!=NULL){
echo '<option value="'.$fila2["Name"].'">'.$fila2["Name"].'</option>';
};
echo "</select>
<input type='submit' value='Enviar'><br>
</form>";
?>
<table border=1>
<tr>
<th>Nombre</th>
<th>Poblacion</th>
</tr>
<?php
/*foreach(){
<tr><td></td><td></td></tr>
}*/
?>
</table>
</body>
</html>