PHP 加入搜索 table

PHP Search with join table

所以我有 2 table :

品牌:

产品:

我想搜索产品并返回该品牌名称,我使用此代码:

   $query = mysqli_query($koneksi,"SELECT product.* FROM brand 
                                     LEFT JOIN product on brand.id = product.id_brand
                                     WHERE product.nama_product LIKE '%".$keyword."%'
                                     OR product.harga_product LIKE '%".$keyword."%'
                                     OR product.berat_product LIKE '%".$keyword."%'
                                     OR product.warna_product LIKE '%".$keyword."%' 
                                     ORDER BY id ASC");
    $hitung_data = mysqli_num_rows($query);
    if ($hitung_data > 0) {
        while ($data = mysqli_fetch_array($query)) {
            ?>
            <tr>
                <td><?php echo $no++; ?></td>
                <td><?php echo $data['nama_product']; ?></td>
                <td><?php echo $data['brand_name']; ?></td>
                <td><?php echo $data['harga_product']; ?></td>
                <td><?php echo $data['berat_product']; ?></td>
                <td><?php echo $data['warna_product']; ?></td>
            </tr>
        <?php } } else { ?> 
            <tr>
                <td colspan='4' class="text-center">Tidak ada data ditemukan</td>
            </tr>
        <?php } ?>

然后突然出现这样的错误:

Notice: Undefined index: brand_name in C:\xampp\htdocs\Project-Joki\tugaspw2022\admin\dashboard\product_data.php on line 37

我该如何解决?

brand_namebrand table 的一部分,因此您也需要 select 才能获得结果。而不是:

SELECT product.* FROM brand 
LEFT JOIN product on brand.id = product.id_brand
WHERE product.nama_product LIKE '%".$keyword."%'
OR product.harga_product LIKE '%".$keyword."%'
OR product.berat_product LIKE '%".$keyword."%'
OR product.warna_product LIKE '%".$keyword."%' 
ORDER BY id ASC

使用:

SELECT product.*, brand.* FROM brand 
LEFT JOIN product on brand.id = product.id_brand
WHERE product.nama_product LIKE '%".$keyword."%'
OR product.harga_product LIKE '%".$keyword."%'
OR product.berat_product LIKE '%".$keyword."%'
OR product.warna_product LIKE '%".$keyword."%' 
ORDER BY id ASC