-1

プロジェクト用のミニ ショッピング カートを作成しています。ユーザーが選択したアイテムの数を保存していますが、セッション変数に1つ追加すると、最初の行で常にこのエラーが発生することを理解していません Undefined index: cart_1 in D:\wamp\www\MiniCart\cart. php on line 100 そして、同じページを再度追加または更新すると、正常に動作します。このエラーが発生するのはなぜですか? ステートメントからを削除した+=1ところ、問題なく動作しました。構文エラーもないようです。

Cart.php

<!DOCTYPE>
<html>
<head></head>
<body>
<?php
session_start();

//The page where to jump to after adding/editing cart.
$page = 'mini_cart_index.php';

$link = mysqli_connect("localhost","root","","cart");
if(mysqli_connect_errno())
{
    echo "Error:".mysqli_connect_error();
    echo "<br/>";
} else {
    echo "Connected to SQL<br/>";
}

//==================================================
if(isset($_GET['add']))
{
    $obt=$_GET['add'];
    $quantity_limit = 'SELECT id,quantity FROM products WHERE id='.mysqli_real_escape_string($link,(int)$_GET['add']);

    $quantity = mysqli_query($link,$quantity_limit);

    while($quantity_row=mysqli_fetch_assoc($quantity))
    {
        if($quantity_row['quantity']!=$_SESSION['cart_'.$_GET['add']])
        {
            $_SESSION['cart_'.$_GET['add']]+='1';
        }
    }

    /*  
    echo 'id='.$obt.' '.'next<br/>';
    echo 'Now storing info into session variable and adding one<br/>';
    echo $_SESSION['cart_'.$_GET['add']];
    echo '<br/>';
    echo 'info stored<br/>';
    */  
}

//***************************************************
function products()
{
    GLOBAL $link;

    $get ="SELECT id,name,description,price FROM products
    WHERE quantity > 0 ORDER by id ASC";

    if($result=mysqli_query($link,$get))
    {
        echo "Data Selected to be displayed<br/>";
    } else {
        echo "Error:".mysqli_error($link);
    }

    if(mysqli_num_rows($result)==0)
    {
        echo "There are no products to display!<br/>";
    } else {
        while($get_row=mysqli_fetch_assoc($result))
        {
            echo '<hr/><br/>';  
            echo 'displaying data from database<br/>';
            echo '==================================';

            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 '<hr/><br/>';

        }
    }   
}

echo 'outside'.$_SESSION['cart_1'];
?>

</body>
</html>

Mini_cart_index.php

<?php require 'cart.php';?>
<!DOCTYPE>
<html>
<head>
</head>
<body>
<?php products() ?>
</body>
</html>
4

3 に答える 3