注意:未定义的索引:cart_5

时间:2016-01-31 07:21:59

标签: php mysql html5

我一直收到这个错误:

  

注意:未定义的索引:第47行的I:\ xxxxx \ xxxxx \ xxxxx \ tst \ cart.php中的cart_5

但视频中的人没有:

  

https://www.youtube.com/watch?v=D40bjSBeU6k&list=PL4AXXo9KTKTCq8Nq0NUmVof3riigdS4yR&index=3

<?php
session_start();

$page = "products.php";

mysql_connect('localhost','xxx','xxxxx') or die(mysql_error());
mysql_select_db('xxxxxx') or die(mysql_error());

if (isset($_GET['add'])) {
    $quantity = mysql_query('SELECT id, inventory
        FROM product
        WHERE id=' .mysql_real_escape_string
        ((int)$_GET['add']));
    while ($quantity_row = mysql_fetch_assoc($quantity)) {
        if ($quantity_row['inventory']!=$_SESSION['cart_'.$_GET['add']]) {
            $_SESSION['cart_'.$_GET['add']]+='1';
        }
    }                         
}

function products() {
    $get = mysql_query('SELECT id, name, Description, price 
        FROM product    
        WHERE inventory > 0
        ORDER BY id DESC');
    if (mysql_num_rows($get)==0) {
        echo "No products in cart";
    } else {
        WHILE ($get_row = mysql_fetch_assoc($get)) {
            echo '<p>'
                .$get_row['name']
                .'<br />'
                .$get_row['Description']
                .'<br />'
                .number_format($get_row['price'], 2)
                .'<a href="cart.php?add='.$get_row['id'].'">Add</a>.
                </p>';
        }
    }
}

echo $_SESSION['cart_5'];
?>

1 个答案:

答案 0 :(得分:0)

<?php
session_start();

$page = "products.php";

mysql_connect('localhost','xxxx','xxxx') or die(mysql_error());
mysql_select_db('xxxxx') or die(mysql_error());

if (isset($_GET['add'])) {
    $quantity = mysql_query('SELECT id, inventory
        FROM product
        WHERE id=' .mysql_real_escape_string
        ((int)$_GET['add']));
    while ($quantity_row = mysql_fetch_assoc($quantity)) {
        if ($quantity_row['inventory']!=$_SESSION['cart_'.$_GET['add']]) {
            $_SESSION['cart_'.$_GET['add']]+='1';
        }
    }                         
}

function products() {
    $get = mysql_query('SELECT id, name, Description, price 
        FROM product    
        WHERE inventory > 0
        ORDER BY id DESC');
    if (mysql_num_rows($get)==0) {
        echo "No products in cart";
    } else {
        while ($get_row = mysql_fetch_assoc($get)) {
            echo '<p>'
                .$get_row['name']
                .'<br />'
                .$get_row['Description']
                .'<br />'
                .number_format($get_row['price'], 2)
                .'<a href="cart.php?add='.$get_row['id'].'">Add</a>'
                .'</p>';
        }
    }
}

echo $_SESSION['cart_6'];
?>