php - 注意:未定义索引product_name

标签 php html mysql undefined-index notice

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

相关文章:

php - 保留管理页面来管理数据库是否危险?

php - 在 ubuntu 中通过 php 脚本发送邮件

mysql alter table 会锁表吗?

mysql - 1615 准备好的语句需要在 codeigniter 中重新准备

mysql - 如何从另一台PC在线连接到MySQL数据库?网络

javascript - 由另一个 ajax 函数触发的 Ajax 函数不起作用

html - 当不需要在嵌入到另一个 html 页面的 html 页面上滚动时删除滚动条

image - Coldfusion 拒绝解码 Base64 图像

html - 多个页面上的 Jekyll 分页

php - 显示数据库中选中的复选框记录