Php 为什么我会犯这样的错误,一切看起来都是对的;未定义索引“;?

Php 为什么我会犯这样的错误,一切看起来都是对的;未定义索引“;?,php,mysql,Php,Mysql,我在这里不知所措。我一辈子也弄不明白为什么我会犯这样的错误 Notice: Undefined index: price in /home/*****/public_html/newfinal/home_view.php on line 14 Notice: Undefined index: description in /home/****/public_html/newfinal/home_view.php on line 15 以下是相关的代码片段: PHP/SQL function

我在这里不知所措。我一辈子也弄不明白为什么我会犯这样的错误

Notice: Undefined index: price in /home/*****/public_html/newfinal/home_view.php on line 14

Notice: Undefined index: description in /home/****/public_html/newfinal/home_view.php on line 15
以下是相关的代码片段:

PHP/SQL

function get_product($product_id) {
    global $db;
    $query = "SELECT * FROM prjProducts p"
            . " INNER JOIN prjCategories c"
            . " ON p.categoryID = c.categoryID"
            . " WHERE productID = :product_id";
    try {
        $stmt = $db->prepare($query);
        $stmt->bindValue(':product_id', $product_id);
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $stmt->execute();
        $result = $stmt->fetchAll();
        $stmt->closeCursor();
        return $result;
    } catch (PDOException $ex) {
        $error = $ex->getMessage();
        echo $error;
    }
}
PHP

HTML


  • 首先检查表
    prjProducts
    中是否有
    price
    description

  • foreach($products as$product)
    循环中,对
    $product
    执行
    var_dump
    并检查它是
    数组还是
    对象


  • 如果它不是一个数组,你应该做
    $product->price
    而不是
    $product['price']

    你在
    prjProducts
    表中有哪些列?请打印你的$products数组并显示@harlanuse var_dump($products)和print_r($products)以查看你的实际数据getting@masterFly
    productID,categoryID,productName,description,price,imgFile
    这里可能重复的是
    var\u dump
    array(1){[0]=>array(7){[“productID”]=>string(1)“3”[“categoryID”]=>string(1)“4”[“productName”]=>string(44)“Gerber-Swagger辅助打开小刀”[“description”]=>string(125)“Gerber Swagger辅助开启式袖珍刀提供双G-10和不锈钢手柄,握力和耐用性极佳。”[“价格”]=>string(5)“39.99”[“imgFile”]=>string(16)“gerberpocket.jpg”[“categoryName”]=>string(13)“袖珍刀”}
    这是产品的var转储,对吗?不是产品?这是foreach循环中$product的var\u转储。我想我解决了问题,但不是解决方案。我运行了一个
    打印($products)
    直接位于第一个foreach函数之后。它看起来好像是一个三级数组而不是2级数组。第二级数组没有索引号?为什么会这样?我该如何修复它?因此,如果这是单个$product的转储,则$product['price']不存在。因为详细信息在另一个数组中。因此它应该是$product[0][价格]
    // Set the featured product Id's in an array
    $product_ids = array(3, 5, 10);
    
    // Get an array of featured producst from the database
    $products = array();
    foreach ($product_ids as $product_id) {
        $product = get_product($product_id);
        $products[] = $product; // add the product to the array
    
    }
    
    <?php foreach ($products as $product) :
              // Get product data
              $price = $product['price'];
              $description = $product['description'];