Get data from MySql to a drop-down list (select option)

0

I have this code for a select option and it consults the database and brings me the logged in user, but I need to list all the users that I have in the database.

I'm not even storing it in the database when choosing the only user that brings me.

                       <label>
                            Usuario    
                            <?php
                                $usuarios = $database->select("usuario", [ "id_usuario", "nombre", "nick"], ["nombre" => $data['nombre']]);
                                if ($usuarios) {
                                    echo "<span class='obligatorio'>*</span><select id='usuario' name='usuario' class='width-100' required>";
                                    echo "<option></option>";
                                        foreach ($usuarios as $usuario) 
                                             echo "<option value=".$usuario['nombre'].">".$usuario['nick']."</option>";
                                    echo "</select>";
                                } else echo "<select class='width-100' disabled><option></option></select>";
                            ?>
                        </label>
    
asked by Alex Milano 15.06.2017 в 20:09
source

2 answers

1

I had the same problem and my solution was to make the direct query .. ejm

introducir el código aquí  $sql = "SELECT id_usuario,nombre,nick FROM usuario" ; $result = mysql_query ($sql);
  while ($usuario= mysql_fetch_row($result)){
           <option value=".$usuario[0].">".$usuario[1]."</option>"; }

It would be better to adjust msqli according to the version of php that it is handling and invoke the connection to the DB !! Good luck ..

    
answered by 15.06.2017 в 21:22
0

HERE I CREATE THE SELECT OPTION

<div class="units-row">
                <div class="unit-30">
                        <label>
                            Usuario    
                            <?php
                                $usuarios = $database->select("usuario", [ "id_usuario", "nombre", "nick"], ["nombre" => $data['nombre']]);
                                if ($usuarios) {
                                    echo "<span class='obligatorio'>*</span><select id='nick' name='nick' class='width-100' required>";
                                    echo "<option></option>";
                                        foreach ($usuarios as $usuario) 
                                             echo "<option value=".$usuario['nick'].">".$usuario['nick']."</option>";
                                    echo "</select>";
                                } else echo "<select class='width-100' disabled><option></option></select>";
                            ?>
                        </label>
                    </div>

HERE I RECEIVE HIM

$nick = htmlspecialchars($_POST["nick"]);

THOSE INSERT IN THE BD

$last_user_id = $database->insert("inventario", [
    "nick"   => $nick]);
    
answered by 15.06.2017 в 22:32