-1

i m geting this error in my code

Undefined index: categories

Notice: Undefined index: categories in C:\xampp\htdocs\project\admin\products.php on line 14

this is line 14

$child_id = $product['categories'];

and this is whole code

<?php 
require_once $_SERVER['DOCUMENT_ROOT'].'/project/core/init.php';
include 'includes/head.php';
include 'includes/navigation.php';
$sql = "SELECT * FROM Products WHERE deleted = 0";
$presults = $db->query($sql);
?>
<h2 class="text-centre" style="text-align: center;">Products</h2><hr>
<table class="table table-boedered table-condensed table-striped">
<thead><th></th><th>Products</th><th>Price</th><th>Category</th><th>Featured</th><th>Sold</th></thead>
<tbody>
    <?php while($product = mysqli_fetch_assoc($presults)): 
          // Trying to display the parent category for every product
               $child_id = $product['categories'];
               $catSql = "SELECT * FROM categories WHERE id = '$child_id'";
               $result = mysqli_query($db, $catSql);
               $child = mysqli_fetch_assoc($result);
               $parentID = $child['parent_id'];
               $pSql = "SELECT * FROM categories WHERE id = '$parentID'";
               $presult = mysqli_query($db, $pSql);
               $parent = mysqli_fetch_assoc($presult);
               $category = $parent['category'] . '-' . $child['category'];
               // Idea for future. Will have nested categories. Only display immediate parent category of product
          ?>
     <tr>
        <td>
            <a href="Products.php?edit=<?=$Product['id'];?>" class="btn btn-xs btn-defualt"><span class="glyphicon glyphicon-pencil"></span></a>
            <a href="Products.php?delete=<?=$Product['id'];?>" class="btn btn-xs btn-defualt"><span class="glyphicon glyphicon-remove"></span></a>
        </td>
        <td><?=$product['title']; ?></td>
        <td><?=money($product['price']);?></td>
        <td><?=$category; ?></td>
        <td>o</td>
     </tr>
    <?php endwhile; ?>
</tbody>
</table>



 <?php include 'includes/footer.php'; ?>

What is this mean and how to work around it

thanks in advance

Imran Rafique
  • 1,986
  • 2
  • 12
  • 14

1 Answers1

0

That seems to indicate your Product table doesn't have a categories column. Verify by adding this, before line 14:

var_dump($product);

You will then see the $product array you can work with, and you can verify where you should get the category ID from.

Guillaume Boudreau
  • 2,676
  • 29
  • 27