当我的购物车为空时它会显示此警告注意:未定义索引:第 57 行 C:\xampp\htdocs\online shop\user\checkout.php 中的购物车

It show this warning when my shopping cart is empty Notice: Undefined index: cart in C:\xampp\htdocs\online shop\user\checkout.php on line 57

我想做一个电子商务网站。但我的问题是在购物车。当我的购物车为空时,它会显示这种类型的消息:(注意:未定义的索引:第 57 行 C:\xampp\htdocs\online shop\user\checkout.php 中的购物车) 我需要一些建议如何解决这个问题。如果我将此 "cart" 分配为 null 或 0,那么我的购物车将无法正常工作。我需要准确的建议。

这是我的checkout.php页面(这里是57号线)

<?php session_start();
 require_once("../dataAccessLayer/dalSession.php");
 require_once("../dataAccessLayer/dalProduct.php");
 require_once("../dataAccessLayer/dalService.php");
 ?>

<?php include ("../header2.php");?>

<br>

 <div class="w3-content " style="max-width:1400px; margin-top:40px;">

 <!-- The Grid -->
  <div class="w3-row-padding">

 <div class="w3-container w3-card-2 w3-white w3-margin-bottom ">

<br>
    <div class="container">

<h1>Shopping Cart</h1><hr>
<table class="table table-striped table-hover table-bordered">
    <tbody>
    <tr>
        <th>Product</th>
        <th>Price</th>
        <th>Vat</th>
        <th>Discount</th>
        <th>Quantity</th>
        <th>Total Price</th>
        <th>Delete</th>
    </tr> 

    <?php

    if(isset($_POST['sub']))
    {

        for($i=0; $i<count($_SESSION['cart']); $i++)
        {
            //print "for loop in";
            if(isset($_POST['qty_' . $_SESSION['cart'][$i]]))
            {

                //print $_POST['qty_' . $_SESSION['cart'][$i]];
                $_SESSION['qty'][$i] = $_POST['qty_'.$_SESSION['cart']                                     [$i]]; 
            }
        }

    }



    $total = 0;
    //$_SESSION['cart']=[];
   //if(isset($_POST['sub'])){      
        for($i=0; $i<count($_SESSION['cart']); $i++) //line 57
        {
            $p = new Product();
            $p->product_id = $_SESSION['cart'][$i];
            $r = $p->SelectById();
            print "<tr>";
  print "<td align=\"center\"><img style=\"width: 50px;height: 50px;\"        src=\"../image/$r[5]\"></td>";
                echo "<td align=\"center\">$r[2]"," TK","</td>";
                echo "<td align=\"center\">$r[3]","%","</td>";
                echo "<td align=\"center\">$r[4]","%","</td>";
        print "<td align=\"center\">";

    ?>
                    <form action="" method="post">
                      <input type="text" name="qty_<?php print                             $_SESSION['cart'][$i];?>" value="<?php print $_SESSION['qty'][$i];?>">
                      <input type="submit" name="sub" value="Update">
                    </form>
    <?php               
                print "</td>";
                print "<td align=\"center\">";
                       $price = $r[2] * $_SESSION['qty'][$i];               
                       echo "TK ".$t= $price + ($price * $r[3])/100 -     ($price * $r[4])/100; 
                       $total += $t;  
                print "</td>";
                print "<td align=\"left\"><a class=\"btn btn-danger\"                    href=\"cart_remove.php?id={$_SESSION['cart'][$i]}\">Remove</a></td>";

            print "</tr>";

    }   


    ?>

                <form action="purchase.php" method="post">

                    <tr>
                        <th colspan="5"><span class="pull-right">Total</span></th>
                        <th><?php print "".$total." TK"; Session::Set("total",$total); ?></th>
                    </tr>
                    <tr>

                        <td colspan="6"><a href="../index.php" class="btn    btn-primary">Continue Shopping</a></td>
                       <td colspan="5"><button type="submit" class="btn   btn-success" name="sub" value="Confirm">
                        Checkout <span class="glyphicon glyphicon-play">    </span></button>
                       </td>
                    </tr>   
                </form>
</table>
</div>
<br>
</div>
</div>
</div>

<?php include ("footer.php");?>

这是我的 add_to_cart.php 页面

<?php
require_once("../dataAccessLayer/dalSession.php");

 Session::Start();

if(isset($_GET['id']))
{

if(isset($_SESSION['cart']))
{
    $c=0;
    for($i=0; $i<count($_SESSION['cart']); $i++)
    {   
        if($_SESSION['cart'][$i] == $_GET['id'])
        {
            $c++;
            break;
        }   
    }
    if($c == 0)
    {
        $_SESSION['cart'][] = $_GET['id'];
        $_SESSION['qty'][] = 1;
    }

  }
  else
  {
    $_SESSION['cart'][] = $_GET['id'];
    $_SESSION['qty'][] = 1;     
  }

  }
  header("Location: details.php?id={$_GET['id']}");

  ?>

这是我的dalSession.php页面:这是用于检查登录和会话。

  <?php

  class Session
  {
  public static function Start(){
    session_start();
  }

   public static function Set($key , $value){
    $_SESSION[$key] = $value;
  }

   public static function Get($key){
    if (isset($_SESSION[$key])) {
        return $_SESSION[$key];
    }
    else{
        return false;
     }
   }

    public static function Stop(){
    session_destroy();
    header("Location:user/login.php");
   }

   public static function StopA(){
    session_destroy();
    header("Location:../user/login.php");
   }

   public static function StopB(){
    session_destroy();
    header("Location:login.php");
   }


    public static function Check()
   {
    self::Start();
    //echo $this->user_id;;
    if (self::Get("Mlogin")==false) 
    {
        self::Stop();
        header("Location:login.php");
    }
    }

    public static function CheckA(){
    self::Start();
    if (self::Get("Alogin")==false) {
       self::StopA();
       header("Location:../user/login.php");
    }
    }

    public static function CheckAll()
    {
    if (self::Get("Mlogin")==false && self::Get("Alogin")==false) 
    {
        return false;
    }
    else
    {
        return true;    
    }    
    }

    public static function CheckUserLogin()
    {self::Start();
    if (self::Get("Mlogin")==false) 
    {
        return false;
    }
    else
    {
        return true;    
    }    
    }

    public static function Auto(){
    self::Start();
    if(self::Get("Mlogin")==false){
       echo "<a style=\"color:white;\" href='user/login.php'>Login</a>";
    } else {
       echo "<a style=\"color:red;\" href='?action=logout'>Logout</a>";
      if(isset($_GET['action']) && ($_GET['action']== "logout")){
        self::Stop();
    }
     }

    }

    public static function AutoA(){
    self::Start();
    if(self::Get("Mlogin")==false){
       echo "<a style=\"color:white;\" href='login.php'>Login</a>";
    } else {
       echo "<a style=\"color:red;\" href='?action=logout'>Logout</a>";
      if(isset($_GET['action']) && ($_GET['action']== "logout")){
        self::StopB();
       }
       }

       }


       }

      ?>

在对会话做任何事情之前检查它是否为空
您也可以使用 array_key_exists('cart',$_SESSION) 来检查会话是否已设置

if(isset($_SESSION['cart']) && !empty($_SESSION['cart'])) {
   echo 'not empty';
   //do what you want to with your session value here
}

if(!empty($_SESSION['cart']) && array_key_exists('cart',$_SESSION)) {
   echo 'not empty';
   //do what you want to with your session value here
}

在你的checkout.php

<?php session_start();
 require_once("../dataAccessLayer/dalSession.php");
 require_once("../dataAccessLayer/dalProduct.php");
 require_once("../dataAccessLayer/dalService.php");
?>
<?php include ("../header2.php");?>


<br>

<div class="w3-content " style="max-width:1400px; margin-top:40px;">

<!-- The Grid -->
<div class="w3-row-padding">

<div class="w3-container w3-card-2 w3-white w3-margin-bottom ">

<br>
    <div class="container">

<h1>Shopping Cart</h1><hr>
<table class="table table-striped table-hover table-bordered">
    <tbody>
    <tr>
        <th>Product</th>
        <th>Price</th>
        <th>Vat</th>
        <th>Discount</th>
        <th>Quantity</th>
        <th>Total Price</th>
        <th>Delete</th>
    </tr> 

    <?php

    if(isset($_POST['sub']))
    {

        for($i=0; $i<count($_SESSION['cart']); $i++)
        {
            //print "for loop in";
            if(isset($_POST['qty_' . $_SESSION['cart'][$i]]))
            {

                //print $_POST['qty_' . $_SESSION['cart'][$i]];
                $_SESSION['qty'][$i] = $_POST['qty_'.$_SESSION['cart'][$i]]; 
            }
        }

    }



    $total = 0;
    //$_SESSION['cart']=[];
   //if(isset($_POST['sub'])){  

        for($i=0; $i<count($_SESSION['cart']); $i++) //line 57
        {
            $p = new Product();
            $p->product_id = $_SESSION['cart'][$i];
            $r = $p->SelectById();
            print "<tr>";
    print "<td align=\"center\"><img style=\"width: 50px;height: 50px;\" 
    src=\"../image/$r[5]\"></td>";
                echo "<td align=\"center\">$r[2]"," TK","</td>";
                echo "<td align=\"center\">$r[3]","%","</td>";
                echo "<td align=\"center\">$r[4]","%","</td>";
        print "<td align=\"center\">";

    ?>
                    <form action="" method="post">
                      <input type="text" name="qty_<?php print 
     $_SESSION['cart'][$i];?>" value="<?php print $_SESSION['qty'][$i];?>">
                      <input type="submit" name="sub" value="Update">
                    </form>
    <?php               
                print "</td>";
                print "<td align=\"center\">";
                       $price = $r[2] * $_SESSION['qty'][$i];               
                       echo "TK ".$t= $price + ($price * $r[3])/100 - 
      ($price * $r[4])/100; 
                       $total += $t;  
                print "</td>";
                print "<td align=\"left\"><a class=\"btn btn-danger\" 
      href=\"cart_remove.php?id={$_SESSION['cart'][$i]}\">Remove</a></td>";

            print "</tr>";


       }

    ?>

                <form action="purchase.php" method="post">

                    <tr>
                        <th colspan="5"><span class="pull-
   right">Total</span></th>
                        <th><?php print "".$total." TK"; 
     Session::Set("total",$total); ?></th>
                    </tr>
                    <tr>

                        <td colspan="6"><a href="../index.php" class="btn 
     btn-primary">Continue Shopping</a></td>
                       <td colspan="5"><button type="submit" class="btn btn-
    success" name="sub" value="Confirm">
                        Checkout <span class="glyphicon glyphicon-play">
   </span></button>
                       </td>
                    </tr>   
                </form>
  </table>
 </div>
 <br>
 </div>
 </div>
 </div>

 <?php include ("footer.php");?>

只需添加此代码:

<?php session_start();
 require_once("../dataAccessLayer/dalSession.php");
 require_once("../dataAccessLayer/dalProduct.php");
 require_once("../dataAccessLayer/dalService.php");
?>
<?php include ("../header2.php");?>


<br>

 <div class="w3-content " style="max-width:1400px; margin-top:40px;">

<!-- The Grid -->
 <div class="w3-row-padding">

 <div class="w3-container w3-card-2 w3-white w3-margin-bottom ">

  <br>
    <div class="container">

  <h1>Shopping Cart</h1><hr>
  <table class="table table-striped table-hover table-bordered">
    <tbody>
    <tr>
        <th>Product</th>
        <th>Price</th>
        <th>Vat</th>
        <th>Discount</th>
        <th>Quantity</th>
        <th>Total Price</th>
        <th>Delete</th>
    </tr> 

    <?php

    if(isset($_POST['sub']))
    {

        for($i=0; $i<count($_SESSION['cart']); $i++)
        {
            //print "for loop in";
            if(isset($_POST['qty_' . $_SESSION['cart'][$i]]))
            {

                //print $_POST['qty_' . $_SESSION['cart'][$i]];
                $_SESSION['qty'][$i] = $_POST['qty_'.$_SESSION['cart'][$i]]; 
            }
        }

    }



    $total = 0;
    //$_SESSION['cart']=[];
   //if(isset($_POST['sub'])){  
       if(!empty($_SESSION['cart']) && array_key_exists('cart',$_SESSION)) {

        for($i=0; $i<count($_SESSION['cart']); $i++) //line 57
        {
            $p = new Product();
            $p->product_id = $_SESSION['cart'][$i];
            $r = $p->SelectById();
            print "<tr>";
     print "<td align=\"center\"><img style=\"width: 50px;height: 50px;\" 
     src=\"../image/$r[5]\"></td>";
                echo "<td align=\"center\">$r[2]"," TK","</td>";
                echo "<td align=\"center\">$r[3]","%","</td>";
                echo "<td align=\"center\">$r[4]","%","</td>";
        print "<td align=\"center\">";

    ?>
                    <form action="" method="post">
                      <input type="text" name="qty_<?php print 
    $_SESSION['cart'][$i];?>" value="<?php print $_SESSION['qty'][$i];?>">
                      <input type="submit" name="sub" value="Update">
                    </form>
    <?php               
                print "</td>";
                print "<td align=\"center\">";
                       $price = $r[2] * $_SESSION['qty'][$i];               
                       echo "TK ".$t= $price + ($price * $r[3])/100 - 
   ($price * $r[4])/100; 
                       $total += $t;  
                print "</td>";
                print "<td align=\"left\"><a class=\"btn btn-danger\" 
   href=\"cart_remove.php?id={$_SESSION['cart'][$i]}\">Remove</a></td>";

            print "</tr>";

    }   
       }

    ?>

                <form action="purchase.php" method="post">

                    <tr>
                        <th colspan="5"><span class="pull-
   right">Total</span></th>
                        <th><?php print "".$total." TK"; 
   Session::Set("total",$total); ?></th>
                    </tr>
                    <tr>

                        <td colspan="6"><a href="../index.php" class="btn 
   btn-primary">Continue Shopping</a></td>
                       <td colspan="5"><button type="submit" class="btn btn-
  success" name="sub" value="Confirm">
                        Checkout <span class="glyphicon glyphicon-play">
  </span></button>
                       </td>
                    </tr>   
                </form>
 </table>
 </div>
 <br>
 </div>
 </div>
 </div>

 <?php include ("footer.php");?>