注意:未定义索引 product_name
Notice: Undefined index product_name
我有这段代码,我不明白为什么我仍然有这个错误,我已经尝试了所有的解决方案,但我无法解决这个问题:
-注意:未定义索引:product_price
-注意:未定义索引:product_quantity
-注意:未定义索引:product_name。
我检查了所有变量名和数据库字段名,一切正常,但我仍然有这个错误。
<?php
session_start();
$connect = mysqli_connect("localhost", "root", "", "storedb");
if(isset($_POST["add_to_cart"]))
{
if(isset($_SESSION["shopping_cart"]))
{
$item_array_id = array_column($_SESSION["shopping_cart"], "product_id");
if(!in_array($_GET["produs_id"], $item_array_id))
{
$count = count($_SESSION["shopping_cart"]);
$item_array = array(
'product_id'=>$_GET["produs_id"],
'product_name'=>$_POST["hidden_name"],
'product_price'=>$_POST["hidden_price"],
'product_quantity'=>$_POST["cantitate"]
);
$_SESSION["shopping_cart"][$count] = $item_array;
echo '<script>window.location="cart.php"</script>';
}
else
{
echo '<script>alert("Item Already Added")</script>';
}
}
else
{
$item_array = array(
'product_id' => $_GET["produs_id"],
'product_name' => $_POST["hidden_name"],
'product_price' => $_POST["hidden_price"],
'product_quantity' => $_POST["cantitate"]
);
$_SESSION["shopping_cart"][0] = $item_array;
}
}
if(isset($_GET["action"]))
{
if($_GET["action"] == "delete")
{
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
if($values["product_id"] == $_GET["produs_id"])
{
unset($_SESSION["shopping_cart"][$keys]);
echo '<script>alert("Item Removed")</script>';
echo '<script>window.location="cart.php"</script>';
}
}
}
}?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Tech Store</title>
<link rel="shortcut icon" type="image/x-icon" href="favicon.png" />
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="slidestyle.css">
</head>
<body>
<div style="clear:both"></div>
<br />
<h3 style="color: white;">Order Details</h3>
<div class="table-responsive">
<table style="border:2px solid red;">
<tr>
<th width="40%" style="color: white;">Item Name</th>
<th width="10%" style="color: white;">Quantity</th>
<th width="20%" style="color: white;">Price</th>
<th width="15%" style="color: white;">Total</th>
<th width="5%" style="color: white;">Action</th>
</tr>
<?php
if(!empty($_SESSION["shopping_cart"]))
{
$total = 0;
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
?>
<tr>
<td style="color: white;"><?php echo $values["product_name"]; ?></td>
<td style="color: white;"><?php echo $values["product_quantity"]; ?></td>
<td style="color: white;"><?php echo $values["product_price"]; ?> Lei</td>
<td style="color: white;"> <?php echo number_format($values["product_quantity"] * $values["product_price"], 2);?></td>
<td style="color: white;"><a href="cart.php?action=delete&produs_id=<?php echo $values["product_id"]; ?>"><span class="text-danger">Remove</span></a></td>
</tr>
<?php
$total = $total + ($values["product_quantity"] * $values["product_price"]);
}
?>
<tr>
<td style="color: white;" colspan="3" align="right">Total</td>
<td style="color: white;" align="right">$ <?php echo number_format($total, 2); ?></td>
<td></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</body>
</html>
有一件事很奇怪...当购物车尚未设置时使用
$_SESSION["shopping_cart"] = [$item_array];
而不是
$_SESSION["shopping_cart"][0] = $item_array;
如果没有设置自己,如何设置 [0] 键?
我有这段代码,我不明白为什么我仍然有这个错误,我已经尝试了所有的解决方案,但我无法解决这个问题: -注意:未定义索引:product_price -注意:未定义索引:product_quantity -注意:未定义索引:product_name。 我检查了所有变量名和数据库字段名,一切正常,但我仍然有这个错误。
<?php
session_start();
$connect = mysqli_connect("localhost", "root", "", "storedb");
if(isset($_POST["add_to_cart"]))
{
if(isset($_SESSION["shopping_cart"]))
{
$item_array_id = array_column($_SESSION["shopping_cart"], "product_id");
if(!in_array($_GET["produs_id"], $item_array_id))
{
$count = count($_SESSION["shopping_cart"]);
$item_array = array(
'product_id'=>$_GET["produs_id"],
'product_name'=>$_POST["hidden_name"],
'product_price'=>$_POST["hidden_price"],
'product_quantity'=>$_POST["cantitate"]
);
$_SESSION["shopping_cart"][$count] = $item_array;
echo '<script>window.location="cart.php"</script>';
}
else
{
echo '<script>alert("Item Already Added")</script>';
}
}
else
{
$item_array = array(
'product_id' => $_GET["produs_id"],
'product_name' => $_POST["hidden_name"],
'product_price' => $_POST["hidden_price"],
'product_quantity' => $_POST["cantitate"]
);
$_SESSION["shopping_cart"][0] = $item_array;
}
}
if(isset($_GET["action"]))
{
if($_GET["action"] == "delete")
{
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
if($values["product_id"] == $_GET["produs_id"])
{
unset($_SESSION["shopping_cart"][$keys]);
echo '<script>alert("Item Removed")</script>';
echo '<script>window.location="cart.php"</script>';
}
}
}
}?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Tech Store</title>
<link rel="shortcut icon" type="image/x-icon" href="favicon.png" />
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="slidestyle.css">
</head>
<body>
<div style="clear:both"></div>
<br />
<h3 style="color: white;">Order Details</h3>
<div class="table-responsive">
<table style="border:2px solid red;">
<tr>
<th width="40%" style="color: white;">Item Name</th>
<th width="10%" style="color: white;">Quantity</th>
<th width="20%" style="color: white;">Price</th>
<th width="15%" style="color: white;">Total</th>
<th width="5%" style="color: white;">Action</th>
</tr>
<?php
if(!empty($_SESSION["shopping_cart"]))
{
$total = 0;
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
?>
<tr>
<td style="color: white;"><?php echo $values["product_name"]; ?></td>
<td style="color: white;"><?php echo $values["product_quantity"]; ?></td>
<td style="color: white;"><?php echo $values["product_price"]; ?> Lei</td>
<td style="color: white;"> <?php echo number_format($values["product_quantity"] * $values["product_price"], 2);?></td>
<td style="color: white;"><a href="cart.php?action=delete&produs_id=<?php echo $values["product_id"]; ?>"><span class="text-danger">Remove</span></a></td>
</tr>
<?php
$total = $total + ($values["product_quantity"] * $values["product_price"]);
}
?>
<tr>
<td style="color: white;" colspan="3" align="right">Total</td>
<td style="color: white;" align="right">$ <?php echo number_format($total, 2); ?></td>
<td></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</body>
</html>
有一件事很奇怪...当购物车尚未设置时使用
$_SESSION["shopping_cart"] = [$item_array];
而不是
$_SESSION["shopping_cart"][0] = $item_array;
如果没有设置自己,如何设置 [0] 键?