-3

I keep getting this error:

Notice: Undefined index: cart_5 in I:\xxxxx\xxxxx\xxxxx\tst\cart.php on line 47

But the guys in the video don't:

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'];
?>
mmm
  • 7
  • 7

1 Answers1

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'];
?>
Antti29
  • 2,953
  • 12
  • 34
  • 36
mmm
  • 7
  • 7