简体   繁体   English

php上的未定义索引错误

[英]Undefined index error on php

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 ?我收到“注意:未定义索引:品牌在 C:\\folder\\folder\\folder\\folder\\file.php 中”,有人能告诉我这是为什么吗? here's my code below you can see line 8 below这是我的代码,您可以在下面看到第 8 行

<?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);
?>

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'.发生“未定义索引”错误是因为mysqli_fetch_assoc()返回的关联数组不包含名为“brand”的索引。

So you can use var_dump() to take a peek and see the actual contents of $product variable like this, example:因此,您可以使用var_dump()来查看一下 $product 变量的实际内容,例如:

<?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) }注意:未定义索引: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"].显然不包含 ["brand"]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM