Foros del Web » Programando para Internet » PHP »

Consulta a Mysql mediante PHP (Combo)

Estas en el tema de Consulta a Mysql mediante PHP (Combo) en el foro de PHP en Foros del Web. Hola, Realizo una consulta de paises con un combo realizado con Dreamweaver. Este me carga un segundo combo con ciudades. Al clicar el primer combo ...
  #1 (permalink)  
Antiguo 07/11/2010, 19:28
 
Fecha de Ingreso: julio-2010
Ubicación: Cerca Sitges (Barcelona)
Mensajes: 98
Antigüedad: 13 años, 9 meses
Puntos: 4
Consulta a Mysql mediante PHP (Combo)

Hola,
Realizo una consulta de paises con un combo realizado con Dreamweaver.
Este me carga un segundo combo con ciudades.

Al clicar el primer combo de paise ej (España), me abre el segundo combo de ciudades (Barcelona, Madrid)...
Me interesaría que al clicar sobre una ciudad (del segundo), por ejemplo Barcelona, me cargue todas las ciudades de la BD mysql de Barcelona y me las liste.

El código actual en PHP

Un saludo y espero vuestras respuestas,

Muchas gracias.

__________

Código PHP:
Ver original
  1. <?php require_once('../Connections/contactos.php'); ?>
  2. <?php require_once('../Connections/contactos.php'); ?>
  3. <?php require_once('../Connections/contactos.php'); ?>
  4. <?php
  5. if (!function_exists("GetSQLValueString")) {
  6. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  7. {
  8.   if (PHP_VERSION < 6) {
  9.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  10.   }
  11.  
  12.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  13.  
  14.   switch ($theType) {
  15.     case "text":
  16.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  17.       break;    
  18.     case "long":
  19.     case "int":
  20.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  21.       break;
  22.     case "double":
  23.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  24.       break;
  25.     case "date":
  26.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  27.       break;
  28.     case "defined":
  29.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  30.       break;
  31.   }
  32.   return $theValue;
  33. }
  34. }
  35.  
  36. if (!function_exists("GetSQLValueString")) {
  37. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  38. {
  39.   if (PHP_VERSION < 6) {
  40.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  41.   }
  42.  
  43.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  44.  
  45.   switch ($theType) {
  46.     case "text":
  47.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  48.       break;    
  49.     case "long":
  50.     case "int":
  51.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  52.       break;
  53.     case "double":
  54.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  55.       break;
  56.     case "date":
  57.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  58.       break;
  59.     case "defined":
  60.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  61.       break;
  62.   }
  63.   return $theValue;
  64. }
  65. }
  66.  
  67. mysql_select_db($database_contactos, $contactos);
  68. $query_Paises = "SELECT * FROM paises";
  69. $Paises = mysql_query($query_Paises, $contactos) or die(mysql_error());
  70. $row_Paises = mysql_fetch_assoc($Paises);
  71. $totalRows_Paises = mysql_num_rows($Paises);
  72.  
  73. $colname_ciudad = "-1";
  74. if (isset($_POST['id_pais'])) {
  75.   $colname_ciudad = $_POST['id_pais'];
  76. }
  77. mysql_select_db($database_contactos, $contactos);
  78. $query_ciudad = sprintf("SELECT id_ciudad, ciudad FROM ciudades WHERE id_pais = %s", GetSQLValueString($colname_ciudad, "int"));
  79. $ciudad = mysql_query($query_ciudad, $contactos) or die(mysql_error());
  80. $row_ciudad = mysql_fetch_assoc($ciudad);
  81. $totalRows_ciudad = mysql_num_rows($ciudad);
  82.  
  83. mysql_select_db($database_contactos, $contactos);
  84. $query_Recordset1 = "SELECT * FROM contactos";
  85. $Recordset1 = mysql_query($query_Recordset1, $contactos) or die(mysql_error());
  86. $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  87. $totalRows_Recordset1 = mysql_num_rows($Recordset1);
  88. ?>
  89. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  90. <html xmlns="http://www.w3.org/1999/xhtml">
  91. <head>
  92. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  93. <title>Documento sin t&iacute;tulo</title>
  94. </head>
  95.  
  96. <body>
  97. <form id="form1" name="form1" method="post" action="">
  98.   <p>
  99.   <select name="id_pais" id="id_pais" onChange="submit()">
  100.     <option value="" <?php if (!(strcmp("", $_POST['id_pais']))) {echo "selected=\"selected\"";} ?>>Pais</option>
  101.     <?php
  102. do {  
  103. ?><option value="<?php echo $row_Paises['id_pais']?>"<?php if (!(strcmp($row_Paises['id_pais'], $_POST['id_pais']))) {echo "selected=\"selected\"";} ?>><?php echo $row_Paises['pais']?></option>
  104.     <?php
  105. } while ($row_Paises = mysql_fetch_assoc($Paises));
  106.   $rows = mysql_num_rows($Paises);
  107.   if($rows > 0) {
  108.       mysql_data_seek($Paises, 0);
  109.       $row_Paises = mysql_fetch_assoc($Paises);
  110.   }
  111. ?>
  112.     </select>
  113.   </p>
  114.   <p>
  115.     <select name="id_ciudad" id="id_ciudad">
  116.       <option value="" <?php if (!(strcmp("", $_POST['id_ciudad']))) {echo "selected=\"selected\"";} ?>>Ciudad</option>
  117.       <?php
  118. do {  
  119. ?><option value="<?php echo $row_ciudad['id_ciudad']?>"<?php if (!(strcmp($row_ciudad['id_ciudad'], $_POST['id_ciudad']))) {echo "selected=\"selected\"";} ?>><?php echo $row_ciudad['ciudad']?></option>
  120.       <?php
  121. } while ($row_ciudad = mysql_fetch_assoc($ciudad));
  122.   $rows = mysql_num_rows($ciudad);
  123.   if($rows > 0) {
  124.       mysql_data_seek($ciudad, 0);
  125.       $row_ciudad = mysql_fetch_assoc($ciudad);
  126.   }
  127. ?>
  128.     </select>
  129.   </p>
  130. </form>
  131. </body>
  132. </html>
  133. <?php
  134.  
  135.  
  136. mysql_free_result($Recordset1);
  137. ?>

Etiquetas: combo, mysql
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 19:14.