0

When selecting a value in the list (green, flowers, breezes, business, all), I need a script to go to the database, select the products table using the list value as Where and fill the table on the page.

I tried with the code below, but I'm not able to apply it correctly, I don't know much about php.

<select name="produto_condominio">
            <option></option>
            <?php while ($reg = $query->fetch_array()) { ?>
              echo '<option value="'.$row['produto_condominio'].'"> '.$row['produto_condominio']. </option>';
            <?php } ?>
          </select>

Full page code:

<?php
$page_title = 'Produtos';
require_once('includes/load.php');
// Checkin What level user has permission to view this page
page_require_level(2);
?>
<?php include_once('layouts/header.php'); ?>

<?php
$con = new mysqli("localhost", "database", "password", "user") or die(mysql_error());
$query = $con->query("SELECT produto_condominio FROM products");
?>



<div class="row">
  <div class="col-md-12">
    <?php echo display_msg($msg); ?>
  </div>
  <div class="col-md-12">
    <div class="panel panel-default">
      <div class="panel-heading clearfix">
        <div class="pull-right">
          <a href="add_product.php" class="btn btn-primary">Adicionar novo produto</a>
        </div>

        <!-- Select por condominio -->
        <div class="pull-left col-md-6">

          <select class="form-control" name="produto_condominio">
            <option value="Selecione">Selecione um condominio</option>
            <option value="Aguas">Aguas</option>
            <option value="Brisas">Brisas</option>
            <option value="Business">Business</option>
            <option value="Flores">Flores</option>
            <option value="Verde">Verde</option>
            <option value="Todos">Todos</option>
          </select>

          <select name="produto_condominio">
            <option></option>
            <?php while ($reg = $query->fetch_array()) { ?>
              echo '<option value="'.$row['produto_condominio'].'"> '.$row['produto_condominio']. </option>';
            <?php } ?>
          </select>
        </div>

        <!-- Select por condominio -->

      </div>
      <div class="panel-body">
        <table class="table table-bordered">
          <thead>
            <tr>
              <th class="text-center" style="width: 50px;">#</th>
              <th> Nome do Produto </th>
              <th class="text-center" style="width: 10%;"> Condominio </th>
              <th class="text-center" style="width: 10%;"> Fornecedor </th>
              <th class="text-center" style="width: 10%;"> Categoria </th>
              <th class="text-center" style="width: 10%;"> Estoque </th>
              <th class="text-center" style="width: 10%;"> Valor </th>
              <th class="text-center" style="width: 10%;"> Adicionado em: </th>
              <th class="text-center" style="width: 100px;"> Ações </th>
            </tr>
          </thead>

          </tabel>
      </div>
    </div>
  </div>
</div>
<?php include_once('layouts/footer.php'); ?>

Page

BD

BD

Dharman
  • 30,962
  • 25
  • 85
  • 135
Paulo Roberto
  • 1,498
  • 5
  • 19
  • 42
  • show us your table structure in the database. – Lucas Jan 20 '20 at 17:55
  • 1
    You have an error. [`mysql_error()`](https://www.php.net/manual/en/function.mysql-error.php) worked only for the old API. Please consider switching error mode on instead. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Jan 20 '20 at 18:03
  • @Lucas I added the image with the bank structure. – Paulo Roberto Jan 20 '20 at 18:48
  • If you're just getting started with PHP and want to build applications, I'd strongly recommend looking at various [development frameworks](https://www.cloudways.com/blog/best-php-frameworks/) to see if you can find one that fits your style and needs. They come in various flavors from lightweight like [Fat-Free Framework](https://fatfreeframework.com/) to far more comprehensive like [Laravel](http://laravel.com/). These give you concrete examples to work from and guidance on how to write your code and organize your project's files. – tadman Jan 21 '20 at 02:27
  • 1
    @tadman Thanks for the suggestion, I will follow this line of study. – Paulo Roberto Jan 21 '20 at 12:34

2 Answers2

0

You are writing PHP code as simple HTML. Change your select to

<select name="produto_condominio">
    <option></option>
    <?php 
    while ($row = $query->fetch_array()) 
    { 
        echo '<option value="'.$row['produto_condominio'].'"> '.$row['produto_condominio']. '</option>';
    } 
    ?>
</select>

Learn about PHP prepared statements. Your code can be prone to SQL Injection.

Also, you are giving the same name produto_condominio to 2 select. Each form field should have a unique name in the form.

Amanjot Kaur
  • 2,028
  • 4
  • 18
  • 33
0

You are using $row but you placed your database row into $reg with fetch_array().

Try

echo '<option value="'.$reg['produto_condominio'].'"> '.$reg['produto_condominio']. '</option>';

Grumbunks
  • 1,027
  • 1
  • 7
  • 21