-1

I'm having a few problems with my PHP code. I have an undefined index error for a variable, '$total'. I need some help with fixing this. Here is my whole PHP code.

<?php
include('inc/connect.php');
session_start();
?>
<html>
<head>
    <title>Cart</title>
    <link rel='stylesheet' href='css/main.css' />
</head>
<body>
    <?php
    $page = 'index.php';

    if(isset($_GET['add'])){
        $add_id = $_GET['add'];
        $quantity = mysql_query("SELECT id, quantity FROM products WHERE id='$add_id'");
        while($quantity_row = mysql_fetch_assoc($quantity)){
            if($quantity_row['quantity'] !=@$_SESSION['cart_'.$add_id]){
                @$_SESSION['cart_'.$_GET['add']]+='1';
                header('Location: index.php');
            }
            else{
                header('Location: index.php?err=max');
            }
        }

    }

    if(isset($_GET['remove'])){
        $_SESSION['cart_'.(int)$_GET['remove']]--;
        header("Location: index.php");
    }

    if(isset($_GET['delete'])){
        $_SESSION['cart_'.(int)$_GET['delete']]='0';
        header('Location: index.php');
    }

    function products(){
        $get = mysql_query("SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id DESC");
        if(mysql_num_rows($get) == 0){
            echo "There are no products to display.";
        }
        else{
            while($get_row = mysql_fetch_assoc($get)){
                echo '<p>'.$get_row['name'].'<br />'.$get_row['description'].'<br />$'.$get_row['price'].' <a href="cart.php?add='.$get_row['id'].'">Add</a></p>';
            }
        }
    }

    function cart(){
        foreach($_SESSION as $name => $value){
            if($value>0){
                if(substr($name, 0, 5)=='cart_'){
                    $id = substr($name, 5, strlen($name)-5);
                    $get = mysql_query("SELECT id, name, price FROM products WHERE id='$id'");
                    while($get_row = mysql_fetch_assoc($get)){
                        $sub = $get_row['price']*$value;
                        echo $get_row['name'].' x '.$value.' @ $'.number_format($get_row['price'], 2).' = $'.$sub.' <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?delete='.$id.'">[Delete]</a>';
                    }
                }
                $total += $sub; 
            }
        }
        echo '<br />'.$total;
    }

    ?>
</body>

Thanks in advance for any help. I will probably need more help in the near future.

WorldxFree
  • 19
  • 2
  • 6
  • 2
    Oh boy. Your code is vulnerable to SQL injection. I hope you don't plan to use this in production. You should also stop using `mysql_` functions. They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. – Kermit Feb 15 '13 at 22:45
  • 1
    See [who is Bobby Tables?](http://bobby-tables.com/) for more. – Kermit Feb 15 '13 at 22:49
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Jocelyn Mar 29 '13 at 11:49

3 Answers3

1

The very first time that the code tries to do $total+=$sub, $total is not defined.

You need to set $total at the top of the cart() function:

function cart(){
    $total = 0;
    /// rest of code
}
Samuel Cook
  • 16,620
  • 7
  • 50
  • 62
1

In your function cart():

Declare variable $total first.

For example $total = 0 ; and then start incrementing it $total += $sub ;

sybear
  • 7,837
  • 1
  • 22
  • 38
0
<?php
include('inc/connect.php');
session_start();
?>
<html>
<head>
    <title>Cart</title>
    <link rel='stylesheet' href='css/main.css' />
</head>
<body>
    <?php
    $page = 'index.php';

    if(isset($_GET['add'])){
        $add_id = $_GET['add'];
        $quantity = mysql_query("SELECT id, quantity FROM products WHERE id='$add_id'");
        while($quantity_row = mysql_fetch_assoc($quantity)){
            if($quantity_row['quantity'] !=@$_SESSION['cart_'.$add_id]){
                @$_SESSION['cart_'.$_GET['add']]+='1';
                header('Location: index.php');
            }
            else{
                header('Location: index.php?err=max');
            }
        }

    }

    if(isset($_GET['remove'])){
        $_SESSION['cart_'.(int)$_GET['remove']]--;
        header("Location: index.php");
    }

    if(isset($_GET['delete'])){
        $_SESSION['cart_'.(int)$_GET['delete']]='0';
        header('Location: index.php');
    }

    function products(){
        $get = mysql_query("SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id DESC");
        if(mysql_num_rows($get) == 0){
            echo "There are no products to display.";
        }
        else{
            while($get_row = mysql_fetch_assoc($get)){
                echo '<p>'.$get_row['name'].'<br />'.$get_row['description'].'<br />$'.$get_row['price'].' <a href="cart.php?add='.$get_row['id'].'">Add</a></p>';
            }
        }
    }

    function cart(){
    $total = 0;
        foreach($_SESSION as $name => $value){
            if($value>0){
                if(substr($name, 0, 5)=='cart_'){
                    $id = substr($name, 5, strlen($name)-5);
                    $get = mysql_query("SELECT id, name, price FROM products WHERE id='$id'");
                    while($get_row = mysql_fetch_assoc($get)){
                        $sub = $get_row['price']*$value;
                        echo $get_row['name'].' x '.$value.' @ $'.number_format($get_row['price'], 2).' = $'.$sub.' <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?delete='.$id.'">[Delete]</a>';
                    }
                }
                $total += $sub; 
            }
        }
        echo '<br />'.$total;
    }

    ?>
</body>

Fixed the code for you.

Vlad
  • 795
  • 1
  • 12
  • 35