gpt4 book ai didi

php - 注意:未定义索引product_name

转载 作者:行者123 更新时间:2023-11-29 15:25:26 28 4
gpt4 key购买 nike

我有这段代码,但我不明白为什么我仍然有这个错误,我已经尝试了所有解决方案,但无法解决这个问题:-注意:未定义索引: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]键本身没有设置,如何设置?

关于php - 注意:未定义索引product_name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59118919/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com