Fill select with data from a table

0

I am trying to fill select with the data of a table called municipios , but I have not gotten it to work, the code is as follows:

<!doctype html>
<html lang="es">
<body> 
<select name="comboseleccion">
<?php
include 'conexion.php';
$query="SELECT * FROM municipios"
$resultado = mysqli_query($query);
if ($resultado)
while($renglon = mysqli_fetch_array($resultado))
{
$valor=$renglon['municipio'];
echo "<option value=".$valor.">".$valor."</option>\n";
}

?>
</select>
</body>
</html>
  

I clarify that the connection is correct, and verify it.

    
asked by Gustavo07 06.05.2017 в 00:22
source

1 answer

1

I have allowed myself to reproduce your code on a hypothetical value, if you were obtaining data from the BD and reading them properly, there would actually be a select with options.

But said select would not be in accordance with the syntax, according to which the value must be enclosed in quotes. That, although in my case both select are displayed, can be cause of errors in some browsers, or when recovering the value in another site.

Code Play

DEMO

<?php

$resultado = 
array("1"=>"Municipio1", "2"=>"Municipio2", "3"=>"Municipio3");
$html_bien="VALUE CON COMILLA Y SIN NADA DESPUÉS DE <\option> \n";
$html_mal="LOS VALUE NO TIENEN COMILLAS\n";



    if ($resultado) 
    {

        $html_mal.="<select>";
        $html_bien.="<select>";


        foreach ($resultado as $k => $valor)
        {
            $html_bien.='<option value="'.$valor.'">'.$valor.'</option>';
            $html_mal.="<option value=".$valor.">".$valor."</option>\n";
        }
        $html_mal.="</select>";
        $html_bien.="</select>";

        echo $html_bien."\n\n";
        echo $html_mal;
    }
?>

Result

VALUE CON COMILLA Y SIN NADA DESPUÉS DE <\option>

  <select>
    <option value="Municipio1">Municipio1</option>
    <option value="Municipio2">Municipio2</option>
    <option value="Municipio3">Municipio3</option>
  </select> 


LOS VALUE NO TIENEN COMILLAS
  <select>
    <option value=Municipio1>Municipio1</option>
    <option value=Municipio2>Municipio2</option>
    <option value=Municipio3>Municipio3</option>
  </select>
    
answered by 06.05.2017 в 03:18