phpmysqlduplicate-datainvoiceswebshop

Duplicate results from mysql


I'm new to php, but I have already made some query's for my webshop. I have a little problem now with making the invoice. I'm trying put the invoice data from the database in a table in my website, but it duplicates itself.

Product_id | Product name | amount | price | subtotal|
2            kippensoep     1         €3      €3
2            kippensoep     1         €3      €3
2            kippensoep     1         €3      €3
4            Loempia's      1        €10     €10
4            Loempia's      1        €10     €10
4            Loempia's      1        €10     €10
4            Loempia's      1        €10     €10

So as you can see, the product_id , product_names are duplicated. It's strange because in the database it isn't duplicated. Here's the php code.

<?php
  $maxfac1 = mysql_fetch_array(mysql_query("SELECT MAX(transactie.factuur_id) FROM transactie , account WHERE transactie.id = $session_user_id"));

  unset ($maxfac1[0]); 
  $maxfactuur_id = implode (" ",$maxfac1);

  $productinfo = mysql_query("SELECT * FROM producten, transactie, factuur WHERE producten.product_id = factuur.product_id AND factuur.factuur_id = $maxfactuur_id AND factuur.gebruikers_id= $session_user_id");

  $totaal=0;
  $subtotaal=0;

  while ($row = mysql_fetch_array($productinfo))
  {
  ?>
  <tr>
    <td><?php echo $row['product_id']?></td>
    <td><?php echo $row['name'] ?></td>
    <td><?php echo $row['amount'] ?></td>
    <td>€<?php echo $row['price'] ?></td>
    <?php $subtotaal = ($row['price']*$row['amount']);?>
    <td>€<?php echo "$subtotaal" ;$totaal+= $subtotaal;?></td>
  </tr>

  <?php
  }
  ?>
?>

I hope you guys can help me and find a solution.

EDIT session_user_id checks if the user is logged in. Then it returns the id from the user who is logged in.


Solution

  • Try to check rows are unique. And add GROUP BY producten.product_id to your query.