PHP查询 - 未定义的索引

时间:2017-03-12 17:58:44

标签: php

我对我的代码有什么问题感到茫然。我是PHP的初学者,不理解我得到的错误。任何建议将不胜感激。在NetBeans中运行此信息时会显示该信息,但是我收到了我不理解的错误。我正在尝试使用带有购物车功能的PHP构建电子商务网站。

<?php
    session_start();

    // Define product information

    $products = array(

        1 => array(
            'name' => 'Manchester United Jersey 1960 / 61',
            'price' => ' €80',
            'category' => 'Premier League',
            'description' => 'Home team jersey worn in 1960. League Position 7th '
        ), 

        2 => array (
            'name' => 'Real Madrid 1964 / 65',
            'price' => ' €80',
            'category' => 'La Liga',
            'description' => 'Home team jersey worn in the 1964 / 65 season. Champions         League Runners up'
        ),

        3 => array(
            'name' => 'Inter Milan 1965 / 66',
            'price' => ' €80',
            'category' => 'Seria A',
            'description' => 'Home team jersey worn in 1965 / 66. Champions League runners up.'
        ),

        4 => array(
            'name' => 'Liverpool',
            'price' => ' €80',
            'category' => 'Premier League',
            'description' => 'Home team jersey worn in 1965 / 66. Champions League Winners.'
         )    
    );

    echo "<h2 style = 'text-align: center;'> Welcome to RETROFIT </h2>
 <p style = 'text-align: right;'><a href = '/index.php?view_cart=1'> View Cart </a></p>";


    //View a product
    if (isset($_GET['view_product'])) {
        $product_id = $_GET['view_product'];
    //View Cart
    }
    else if(isset($_GET['view_cart'])){
        //Display Site Links
        echo "<p>
          <a href ='./index.php'>  Retrofit </a></p>"; 
      echo "<h3> Your Cart </h3>";     
        //View all Products    
    } 
    else  {
        //Display Site Links
        echo "<p>
        <a href ='./index.php'>  Retrofit </a> ";
        echo "<h3> Our Products </h3>"; 

       //Display Products
       echo "<p>
       <span style = 'font weight: bold;'>" . $products [$product_id]['name'] . "</span><br/>
       <span>" . $products [$product_id]['price'] . " </span><br/>
       <span> " . $products [$product_id] ['description'] . " </span><br />
       <span><a href = '#'> Add to Cart </a></span> <br />    
   </p>";
} 
//else {

    //Display Site Links
    echo "<p>
    <a href ='./index.php'> Welcome to Retrofit </a> </p>";

     echo "<h3> Our Products </h3>";

    echo "<table style  'width: 500px;' cellspacing = '10'>";
    echo "<tr>
    <th style= 'border-bottom: 1px solid #000000;'> Name </th>
    <th style= 'border-bottom: 1px solid #000000;'> Price </th>
    <th style= 'border-bottom: 1px solid #000000;'> Category </th> 
    </tr>";


    //Loop to display all products 

        foreach ($products as $id => $product) {
        echo "<tr >
       <td style= 'border-bottom: 1px solid #000000;'><a href = './index.php?view_product=$id'>" . $product ['name'] . "</a></td>
        <td style= 'border-bottom: 1px solid #000000;'>" . $product ['price'] . "     </td>
       <td style= 'border-bottom: 1px solid #000000;'>" . $product ['category'] . "     </td>
      </tr>";
        }
        echo "</table>";
?> 

1 个答案:

答案 0 :(得分:0)

如果你注意到:

 ?view_product=1

将正确显示并且没有定义get参数的url将不会。

你错过的只是一张支票,不错:

if (isset($product_id)) {
     //Display Products
     echo "<p>
    <span style = 'font weight: bold;'>" . $products [$product_id]['name'] . "</span><br/>
    <span>" . $products [$product_id]['price'] . " </span><br/>
    <span> " . $products [$product_id] ['description'] . " </span><br />
    <span><a href = '#'> Add to Cart </a></span> <br />    
     </p>";
}

为了便于阅读,这里是包含已编辑部分的完整文件:https://hastebin.com/opegibenez.xml