My dropdown does not matter bank options

1

I have a form with a dropdown , which should have as options the users registered in a table of my database.

The connection:

 <?php
    $connection = mysqli_connect("localhost", "root", "", "db_formacao");

    if (mysqli_connect_errno())
    {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
?>

The form:

    <!-- Content -->
<?php 
    require 'conn.php';
    $query = mysqli_query("SELECT NOME FROM colaboradores");
?>

    <div class="container">
        <div class="row">
            <div class="col-lg-12 text-center">
                <h1 style="
                    margin-top:100px;">Inscrição</h1>
                <p> </p>
                <p class="lead"></p>
                <ul class="list-unstyled">
                    <form id="cadastro" method="post" action="banco/updateP.php" style="
                        text-align: left;
                        margin-top:50px;">
                        <div class="col-lg-12">
                            <div class="form-group" style="
                        text-align: left;">
                                <label  for="FORMACAO">Formação: </label>
                                <input  type="text" required class="form-control" id="FORMACAO" name="FORMACAO" value="<?=$row['nome']?>">
                             </div>
                        </div>
                        <div class="col-lg-12">
                            <div class="form-group" method="post" style="
                        text-align: left;">
                                <label  for="TURMA">Turma: </label>
                                <input  type="text" required class="form-control" id="TURMA" name="TURMA">
                             </div>
                        </div>
                        <div class="col-lg-12">
                            <div class="form-group" method="post" style="
                        text-align: left;">
                                <label  for="COLABORADOR">Colaborador: </label>
                                <select  class="form-control" id="COLABORADOR" name="COLABORADOR">
                                <option>Selecione...</option>
                                <?php while($colab = mysql_fetch_array($query)) { ?>
                                <option value="<?php echo $colab['NOME']; ?>"><?php echo $colab['NOME']; ?></option>
                                <?php } ?>
                            </select>
                            </div>
                        </div>
                        <div class="col-lg-12">
                            <div class="form-group" method="post" style="
                        text-align: left;">
                                <label  for="PREVISTO">Status: </label>
                                <input  type="text" required class="form-control" id="PREVISTO" name="PREVISTO" value="Previsto">
                             </div>
                            <div class="form-check form-check-inline disabled">
                                <label class="form-check-label">
                                    <input class="form-check-input" type="radio" name="STATUS" id="STATUS" value="Realizado" disabled> Realizado
                                </label>
                            </div>
                            <div class="">
                                <button type="submit" class="btn btn-primary btn-lg btn-block">Salvar</button>
                            </div>
                        </div>
                    </form>
                </ul>
            </div>
        </div>
    </div> 

The options simply do not appear. Can someone point me where the error is?

    
asked by anonymous 28.08.2017 / 14:16

3 answers

2
<?php 
    require 'conn.php';
    $query = mysqli_query($connection, "SELECT NOME FROM colaboradores");
?>

   ...
   ...

                        <select  class="form-control" id="COLABORADOR" name="COLABORADOR">
                            <option>Selecione...</option>
                            <?php while($colab = mysql_fetch_array($query)) { ?>
                            <option value="<?php echo $colab['NOME']; ?>"><?php echo $colab['NOME']; ?></option>
                            <?php } ?>

                        </select>

If you are interested in using Mysqli :

Make the following changes to your conn.php :

<?php
    $connection = mysqli_connect("localhost", "root", "", "db_formacao");

    if (mysqli_connect_errno())
    {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
?>

Now in your other file, where HTML is:

And later on in your code:

 <?php 
    while ($prod = $query->fetch_array()) {
     echo '<option value="' . $prod['NOME'] . '">' . $prod['NOME'] . '</option>';
    }
?>
    
28.08.2017 / 14:28
1

You did not include names in <option> . The correct would be:

<option value="<?php echo $colab['NOME'] ?>"><?php echo $colab['NOME'] ?></option>

Usually an "id" is given from the bank for each value of <option> , type:

<option value="<?php echo $colab['id'] ?>"><?php echo $colab['NOME'] ?></option>

But you also need to select this "id" in the query:

$query = mysql_query("SELECT ID, NOME FROM pacientes");
    
28.08.2017 / 14:37
1

Fixes made using MySQL_i:

  • $query = ("SELECT NOME FROM colaboradores");
  • $results = mysqli_query($connection,$query);
  • <?php while($colab = mysqli_fetch_array($results)) { ?>
  • The corrected form:

    <?php
     require 'conn.php';
    
        $query = ("SELECT NOME FROM colaboradores");
        $results = mysqli_query($connection,$query);
    ?>
    
    
    <div class="container">
        <div class="row">
            <div class="col-lg-12 text-center">
                <h1 style="
                    margin-top:100px;">Inscrição</h1>
                <p> </p>
                <p class="lead"></p>
                <ul class="list-unstyled">
                    <form id="cadastro" method="post" action="banco/updateP.php" style="
                        text-align: left;
                        margin-top:50px;">
                        <div class="col-lg-12">
                            <div class="form-group" style="
                        text-align: left;">
                                <label  for="FORMACAO">Formação: </label>
                                <input  type="text" required class="form-control" id="FORMACAO" name="FORMACAO" value="<?=$row['nome']?>">
                             </div>
                        </div>
                        <div class="col-lg-12">
                            <div class="form-group" method="post" style="
                        text-align: left;">
                                <label  for="TURMA">Turma: </label>
                                <input  type="text" required class="form-control" id="TURMA" name="TURMA">
                             </div>
                        </div>
                        <div class="col-lg-12">
                            <div class="form-group" method="post" style="
                        text-align: left;">
                                <label  for="COLABORADOR">Colaborador: </label>
                                <select  class="form-control" id="COLABORADOR" name="COLABORADOR">
                                <option>Selecione....</option>
                                <?php while($colab = mysqli_fetch_array($results)) { ?>
                                <option value="<?php echo $colab['NOME']; ?>"><?php echo $colab['NOME']; ?></option>
                                <?php } ?>
                            </select>
                            </div>
                        </div>
                        <div class="col-lg-12">
                            <div class="form-group" method="post" style="
                        text-align: left;">
                                <label  for="PREVISTO">Status: </label>
                                <input  type="text" required class="form-control" id="PREVISTO" name="PREVISTO" value="Previsto">
                             </div>
                            <div class="form-check form-check-inline disabled">
                                <label class="form-check-label">
                                    <input class="form-check-input" type="radio" name="STATUS" id="STATUS" value="Realizado" disabled> Realizado
                                </label>
                            </div>
                            <div class="">
                                <button type="submit" class="btn btn-primary btn-lg btn-block">Salvar</button>
                            </div>
                        </div>
                    </form>
                </ul>
            </div>
        </div>
    </div> 
    

    The connection:

    <?php
        $connection = mysqli_connect("localhost", "USUARIO", "SENHA", "db_formacao");
    
        if (mysqli_connect_errno())
        {
           echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
    ?>
    
        
    28.08.2017 / 16:05