My dropdown doesn’t matter bank options

Asked

Viewed 62 times

1

I have a form with a dropdown, that should have as options 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> 

Options simply do not appear. Someone can point me to the error?

  • Hello @Marianaferreira. First, you are using an extension that was discontinued - mysql*.

  • Second point, how the table is modeled collaborators? It has a column in Capslock calling for NAME?

  • The variable you should print would be $Prod and not $colab (which apparently does not exist)

  • Thank you for answering, Godfrey! First point: I use an old version of php and can’t update, so Mysqli doesn’t work so well for me. Second point: Yes, my table has a column in caps called NAME. Third point: I changed the while variable to $colab, but it still doesn’t work.

  • @Marianaferreira I posted a reply before checking that your last comment. I strongly advise on you if possible update to mysqli. If impossible, in my answer, use the extension mysql*.

  • @Marianaferreira Tell me something... You commented that the extension mysqli does not work so well for you... But it is available from version 5 up to PHP. Which version are you using? There’s really no sense in you using the extension mysql*.

  • I use version 5.2.10. It is impossible to upgrade because many systems run in this version and most of it I don’t even have access to.

  • I’ll try to use mysqli

  • Is this application in a production environment? If so, then use mysql* same. If you are in production and the extension does not work, maybe you should compile the PHP with a flag indicating the mysqli... So, I repeat, if you’re in production continue with the mysql.

  • Mari, take a look at my edition. Just replace your select with the one I put.

  • If you are using the above code as it is published, you pairedmysqli_ with mysql. Vela $Connection = mysqli_connect.... E in the while($colab = mysql_fetch_array form

Show 6 more comments

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 modifications to your file 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 is the HTML:


And later in his code:

 <?php 
    while ($prod = $query->fetch_array()) {
     echo '<option value="' . $prod['NOME'] . '">' . $prod['NOME'] . '</option>';
    }
?>
  • So I followed your recommendations, but something very strange happened. After using your code, the whole page after the dropdown just disappeared. I’ll edit with a photo.

  • The page has reappeared! But it is still not importing the options from the bank. :/

  • You know the passage mysql_fetch_array($query)? Comment on that while and instead write var_dump(mysql_fetch_array($query)); to debug your code.

  • Another thing... you’re using now mysqli or mysql? So I can edit my answer.

  • I’m using my mysqli

  • Okay, I’ll edit my reply and leave only the mysqli. You debugged the code as requested?

  • Yeah, but it didn’t work out, so I went back to the while

Show 3 more comments

1

You didn’t include the names in <option>. The Correct would be:

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

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

<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");

1

Fixes made using Mysql_i:

  1. $query = ("SELECT NOME FROM colaboradores");
  2. $results = mysqli_query($connection,$query);
  3. <?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();
    }
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.