Click here to Skip to main content
15,923,051 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have duplicate problem when showing data with 3 different table in php with mysqli ext procedural style. My tables are:

kategori: id_kat, nama_kat

subkategori: id_sub, id_kat, nama_sub

supersubkategori: id_supersub, id_sub, id_kat, nama_supersub, kategori_seo

This is my code:

XML
<?php
    require "../config.php";
    $sql = "SELECT * FROM supersubkategori INNER JOIN kategori ON kategori.id_kat = supersubkategori.id_kat
                        INNER JOIN subkategori ON subkategori.id_kat = supersubkategori.id_kat ORDER BY id_supersub ASC";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0)
    {
        while ($data = mysqli_fetch_array($result))
        {
            echo "<tr class=\"table table-striped\">
                            <td align=\"left\">".$data['id_supersub']."</td>
                            <td align=\"left\">".$data['nama_supersub']."</td>
                            <td align=\"left\">".$data['nama_kat']."</td>
                            <td align=\"left\">".$data['nama_sub']."</td>
                            <td align=\"left\">
                                <a href=\"supersub_ubah.php?id_supersub=$data[id_supersub]\">Edit</a>
                                <a href=\"supersub_hapus.php?id_supersub=$data[id_supersub]\">Hapus</a>
                            </td>
                        </tr>";
        }
    }
        else
        {
            echo "Belum ada data.";
        }
    ?>
Posted
Updated 3-Jun-18 23:00pm
v2

1 solution

First, remove id_kat from supersubkategori table as it is redundant and you can derive it from the subkategori table by id_sub, this concerns http://www.studytonight.com/dbms/database-normalization.php[^]
Next, instead of *, Second, specify the output fields explicitly, e.g.
SQL
SELECT ss.id_supersub, ss.nama_supersub, k.nama_kat, s.nama_sub FROM supersubkategori ss INNER JOIN subkategori s ON s.id_sub=ss.id_sub
INNER JOIN kategori k on s.id_kat = k.id_kat ORDER BY ss.id_supersub ASC
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900