-1

PHP コードにいくつか問題があります。変数 '$total' で未定義のインデックス エラーが発生しました。これを修正するには、助けが必要です。これが私のPHPコード全体です。

<?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>

助けてくれてありがとう。近い将来、おそらくもっと助けが必要になるでしょう。

4

3 に答える 3

1

コードが実行しようとする最初の時間は定義されていません$total+=$sub$total

関数$totalの先頭に設定する必要があります。cart()

function cart(){
    $total = 0;
    /// rest of code
}
于 2013-02-15T22:44:48.220 に答える
1

関数 cart() で:

$total最初に変数を宣言します。

たとえば$total = 0 ; 、次にインクリメントを開始します$total += $sub ;

于 2013-02-15T22:48:53.240 に答える
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>

コードを修正しました。

于 2013-02-16T06:53:14.317 に答える