当我的购物车为空时,显示此警告注意:未定义的索引:购物车在C:\ xampp \ htdocs \ online shop \ user \ checkout.php在线57
问题描述:
我想制作一个电子商务网站。但我的问题是在购物车。当我的购物车是空的,它显示这种类型的消息:(注意:未定义的索引:购物车在C:\ xampp \ htdocs \在线商店\用户\ checkout.php在57行) 我需要一些建议如何解决这个问题。如果我将此“购物车”分配给null或0,那么我的购物车无法正常工作。我需要准确的建议。当我的购物车为空时,显示此警告注意:未定义的索引:购物车在C: xampp htdocs online shop user checkout.php在线57
这是我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();
}
}
}
}
?>
答
在你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");?>
答
在做之前与会话确认任何东西,如果它是空的
您可以使用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
}
OR
if(!empty($_SESSION['cart']) && array_key_exists('cart',$_SESSION)) {
echo 'not empty';
//do what you want to with your session value here
}
'isset()函数'如果你使用'空()' – CD001
是那些替代品是完全没有意义的。 'array_key_exists('cart',$ _ SESSION)&&!empty($ _ SESSION ['cart'])'就够了 – Regolith
请告诉我需要更改哪部分? –