upload mysql data in php combobox

0

Good I am loading mysql data in a combobox, this is my php code that works a lot less well.

<?php  
//CONECTAMOS CON LA BBDD


$conexion = new mysqli("localhost", "root", "", "discografica");
if ($conexion->connect_errno) {
    echo "Fallo al conectar a MySQL: (" . $conexion->connect_errno . ") " . $conexion->connect_error;
}
$sql="SELECT idartistas,nombre from artistas";
$result = $conexion->query($sql);

if ($result->num_rows > 0) //si la variable tiene al menos 1 fila entonces seguimos con el codigo
{
    $combobit="";
    while ($row = $result->fetch_array(MYSQLI_ASSOC)) 
    {
        $combobit .=" <option value='".$row['nombre']."'>".$row['idartistas']."</option>"; 
    }
}
else
{
    echo "No hubo resultados";
}
?>  

and this is a small form that I have

<div class="container">

      <form method="post" role="form" action="insertarcancion.php" >
        <div class="form-group row" action="insertarcancion.php">

            <label for="nombre" class="col-sm-2 form-control-label">artista</label>
            <input    class="form-control" id="artista" placeholder="Escribe tu nombre" type="text" name="nombre">
            <select name="estado">
                <?php echo $combobit; ?>

            </select>
        </div>
        <div class="form-group row" action="insertarartista.php">
            <label for="nombre" class="col-sm-2 form-control-label">Nombre</label>
            <div class="col-sm-10">
              <input class="form-control" id="nombre" placeholder="Escribe tu nombre" type="text" name="nombre">
            </div>
        </div>

        </div>
        <center>
        <div class="form-group">
          <button type="submit" class="btn btn-primary offset-2">Enviar</button>
        </div>
        </center>
      </form>

    </td>
  </tr>

with that I have the following:

link

What I want to achieve is that the data I get from the database is loaded into the input of artists. and the data that they load inside the input, I need them to be idartistas and the name, now only loads the id

    
asked by NuriaOveeeeeeejero 21.02.2018 в 19:00
source

1 answer

0

Invert the data in each option:

$combobit="";
while ($row = $result->fetch_array(MYSQLI_ASSOC)) 
{
    $combobit .=" <option value=\"{$row['idartistas']}\">{$row['nombre']}</option>"; 
}

In this way, the value of the option is idartists and the content name .

Note: To avoid concatenations, you can escape double quotes (by prepending an inverted diagonal) and include the array elements in braces.

    
answered by 21.02.2018 / 19:08
source