-1

so i have 2 table :

Brand :

  • id
  • brand_name

Product :

  • id
  • id_brand (FK)
  • nama_product
  • harga_product
  • warna_product
  • berat_produc

i want to search product and returning that brand name, i use this code :

   $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 } ?>

and suddenly , appear error like this :

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

how do i solve that?

  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman May 30 '22 at 20:01

1 Answers1

1

brand_name is part of the brand table, so you need to select this as well for your results. Instead of:

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

use:

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
KIKO Software
  • 12,609
  • 2
  • 15
  • 29
  • now it's error like this : Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in – Daffa Raka Mahendra May 30 '22 at 15:54
  • @DaffaRakaMahendra: That means there was an error when executing the query. `mysqli_query()` returned `false`. Use `echo mysqli_error($koneksi);`, directly after executing the query, to find out what the problem is. See: [mysqli_error()](https://www.php.net/manual/en/mysqli.error.php) – KIKO Software May 30 '22 at 16:54