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);
?>
ronaldo cr
  • 19
  • 4
  • Does the *products* table have a column named *brand*? – Rajdeep Paul Jan 15 '17 at 00:41
  • yes it does ... – ronaldo cr Jan 15 '17 at 00:45
  • 1
    Then do `var_dump($product);` and check if you have an index named *brand* in that array. – Rajdeep Paul Jan 15 '17 at 00:51
  • here's my full code dear please check https://plnkr.co/edit/1I7BMFWXcpomsRRP0ZYC?p=catalogue – ronaldo cr Jan 15 '17 at 00:53
  • This page should do the trick http://stackoverflow.com/questions/10613570/undefined-index-error-php Modern PHP likes variables to either be declared explicitly e.g. `$brand= "";` before it is used, or checked with the `if(isset($brand... ` structure - you can also use the simpler structure `isset($brand... ? : $brand... = $product...` Lots of useful bits here http://stackoverflow.com/questions/418066/best-way-to-test-for-a-variables-existence-in-php-isset-is-clearly-broken – Steve Jan 15 '17 at 01:01
  • 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. – Rajdeep Paul Jan 15 '17 at 01:12
  • Some more good methods here for satisfying the requirement for variables to have a defined value http://stackoverflow.com/questions/4007752/php-check-if-variable-exist-but-also-if-has-a-value-equal-to-something?rq=1 This has had many heated debates surrounding it and many get quite upset that it broke away from the simplicity of the origins of PHP where variables could be used on the fly - on balance it has its uses in that the notice is intended to make it easier for you to debug misspelled variable names, or variables that have not been set and are being used - so contain no value. – Steve Jan 15 '17 at 01:13
  • @rajdeep-paul ok how can i do that ? – ronaldo cr Jan 15 '17 at 01:46
  • Just put `var_dump($product);` at the end of your PHP code and it will display all of the contents of your `$product` array - which should also be declared as an array `$product = array();` which might help. You should find the index "brand" and its value in the splurge it prints out - or the absence of same. – Steve Jan 15 '17 at 07:22

1 Answers1

1

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"].

Markandeya
  • 399
  • 3
  • 12