While returning duplicate data

1

Good morning guys, I have this query

$query = "SELECT p.*, c.nomeCategoria AS categoria FROM produto AS p INNER JOIN produto_categoria AS c"; 
$dados = mysqli_query($con, $query) or die(mysqli_error());

and a while that returns the value inside a select.

  <select name="categoriaProduto">
  <?php
   while($row = mysqli_fetch_array($dados))
   {
    ?>
  <option value="<?php echo $row["categoria"]; ?>"><?php echo $row["categoria"]; ?></option>
  <?php
 }
  ?>
</select>

In my page it's showing the duplicate select data, I have 3 items registered in the category_product table.

I wanted to know if there was anything I did wrong in the code. Thank you!!

    
asked by anonymous 24.07.2016 / 17:21

1 answer

4
SELECT p.*, c.nomeCategoria AS categoria FROM produto AS p
INNER JOIN produto_categoria AS c

The current query brings the products with associated categories, I imagine there is more than one product with the same category in the table because repeated categories appear.

Correct is to change the query, make a simple select in the category table ( categoria_produto ) and display the result (id, name / description) in the combo.

SELECT idCategoria, nomeCategoria FROM categoria_produto

No php / html:

<option value="<?php echo $row["idCategoria"]; ?>">
   <?php echo $row["nomeCategoria"]; ?>
</option>
    
24.07.2016 / 18:28