1

i am getting "Notice: Undefined index: brand in C:\folder\folder\folder\folder\file.php on line 8" can some one tell me why is this ? here's my code below you can see line 8 below

<?php
require_once '../core/init.php';
$id = $_POST['id'];
$id = (int)$id;
$sql = "SELECT * FROM products WHERE id = '$id'";
$result = $db->query($sql);
$product = mysqli_fetch_assoc($result);
$brand_id = $product['brand'];
$sql = "SELECT brand FROM brand WHERE id = '$brand_id'";
$brand_query = $db->query($sql);
$brand = mysqli_fetch_assoc($brand_query);
?>
asked Jan 15, 2017 at 0:40
9
  • Does the products table have a column named brand? Commented Jan 15, 2017 at 0:41
  • yes it does ... Commented Jan 15, 2017 at 0:45
  • 1
    Then do var_dump($product); and check if you have an index named brand in that array. Commented Jan 15, 2017 at 0:51
  • here's my full code dear please check plnkr.co/edit/1I7BMFWXcpomsRRP0ZYC?p=catalogue Commented Jan 15, 2017 at 0:53
  • 1
    @ronaldocr Raw code wouldn't help us much, You can easily debug this issue just by seeing the array structure. As I said above, do var_dump($product); and check if you have an index named brand in that array. Commented Jan 15, 2017 at 1:12

1 Answer 1

2

Let me explain the error in detail with an example.
'Undefined index' error occurred because the associative array returned by mysqli_fetch_assoc() does not contain an index named 'brand'.

So you can use var_dump() to take a peek and see the actual contents of $product variable like this, example:

<?php
$product = ['name'=>'Item1', 'price'=>10];
$anothervariable = $product['brand'];//which gives the error
//doing a var dump to see the contents of $product
var_dump($product);
?>

Output:

Notice: Undefined index: brand in /var/www/html/test/register.php on line 3 array(2) { ["name"]=> string(5) "Item1" ["price"]=> int(10) }

which obviously doesn't contain ["brand"].

answered Jan 15, 2017 at 4:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.