第47行的索引未定义且foreach arg无效

时间:2019-03-28 13:31:50

标签: php html

在我的购物车页面上,我遇到一个问题,即产品项未显示在网页上,如果我单击“删除项目”按钮,则会出现两个错误,这些错误使我成为了这篇文章的标题。我该怎么做才能克服这些错误?

我的网页看起来像current webpage,而它应该看起来像这样https://i.stack.imgur.com/Xp0xw.png,下面是该页面的代码。

<?php

session_start();
$server = "127.0.0.1";
$dbusername = "root";
$dbpassword = "";
$db = "movie1";


try {
    $handle = new PDO("mysql:host=$server;dbname=$db", "$dbusername", "$dbpassword");
    $handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "connected";
} catch (PDOException $e) {
    die("something failed");
}
if (isset($_POST["add"])){
    if (isset($_SESSION["cart"])){
        $item_array_id = array_column($_SESSION["cart"],"product_id");
        if (!in_array($_GET["id"],$item_array_id)){
            $count = count($_SESSION["cart"]);
            $item_array = array(
                'product_id' => $_GET["id"],
                'item_name' => $_POST["hidden_name"],
                'product_price' => $_POST["hidden_price"],
                'item_quantity' => $_POST["quantity"],
            );
            $_SESSION["cart"][$count] = $item_array;
            echo '<script>window.location="Cart.php"</script>';
        }else{
            echo '<script>alert("Product is already Added to Cart")</script>';
            echo '<script>window.location="Cart.php"</script>';
        }
    }else{
        $item_array = array(
            'product_id' => $_GET["id"],
            'item_name' => $_POST["hidden_name"],
            'product_price' => $_POST["hidden_price"],
            'item_quantity' => $_POST["quantity"],
        );
        $_SESSION["cart"][0] = $item_array;
    }
}

if (isset($_GET["action"])){
    if ($_GET["action"] == "delete"){
        foreach ($_SESSION["cart"] as $keys => $value){ // line 47
            if ($value["product_id"] == $_GET["id"]){
                unset($_SESSION["cart"][$keys]);
                echo '<script>alert("Product has been Removed...!")</script>';
                echo '<script>window.location="Cart.php"</script>';
            }
        }
    }
}
?>

<!doctype html>
 <html>
 <head>
     <meta charset="UTF-8">
     <meta name="viewport"
           content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Shopping Cart</title>


     <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
     <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
     <style>
         .product{
             border: 1px solid #eaeaec;
             margin: -1px 19px 3px -1px;
             padding: 10px;
             text-align: center;
             background-color: #efefef;
         }
         table, th, tr{
             text-align: center;
         }
         .title2{
             text-align: center;
             color: #66afe9;
             background-color: #efefef;
             padding: 2%;
         }
         h2{
             text-align: center;
             color: #66afe9;
             background-color: #efefef;
             padding: 2%;
         }
         table th{
             background-color: #efefef;
         }

     </style>

 </head>
<body>
<div class="container" style="width: 65%">
    <h2>Ticket Cart</h2>
    <?php
    $query = $handle->query('SELECT * FROM product ORDER BY id ASC');

        while ($row = $query->fetch(PDO::FETCH_ASSOC)){


    }

    ?>

    <div class="w3-row-padding w3-padding-16 w3-center">
        <form method="post" action="cart.php?action=add&id=<?php echo $row ["id"];?>">
            <div class="product">
                <img src="<?php echo $row['image']; ?>" class="img-responsive">
                <h5 class = "text-info"> <?php $row['pname']; ?> </h5>
                <h5 class="text-danger"><?php $row['price']; ?></h5>
                <input type="text" name="quantity" class="form-control" value="1">
                <input type="hidden" name="hidden_name" value="<?php echo $row['pname']; ?>">
                <input type="hidden" name="hidden_price" value="<?php echo $row['price']; ?>">
                <input type="submit" name="add" style="margin-top: 5px;" class="btn btn-success"
                       value="Add to Cart">


            </div>


        </form>
    </div>



    <div style="clear: both"></div>
    <h3 class="title2"> Cart Details</h3>
    <div class="table-responsive">
        <table class="table table-bordered">
            <tr>
                <th width="30%">Product Name</th>
                <th width="10%">Quantity</th>
                <th width="13%">Price Details</th>
                <th width="10%">Total Price</th>
                <th width="17%">Remove Item</th>
            </tr>

            <?php
            if(!empty($_SESSION["cart"])){
            $total = 0;
            foreach ($_SESSION["cart"] as $key => $value) {
            ?>
            <tr>
                <td><?php echo $value["item_name"]; ?></td>
                <td><?php echo (int)$value["item_quantity"]; ?></td>
                <td>$ <?php echo (float)$value["product_price"]; ?></td>
                <td>
                    $ <?php echo number_format((int)$value["item_quantity"] * (float)$value["product_price"], 2); ?></td>
                <td><a href="Cart.php?action=delete&id=<?php echo $value["product_id"]; ?>"><span
                            class="text-danger">Remove Item</span></a></td>

            </tr>
            <?php
            $total = $total + ($value["item_quantity"] * (float)$value["product_price"]);
            }
            ?>
            <tr>
                <td colspan="3" align="right">Total</td>
                <th align="right">$ <?php echo number_format($total, 2); ?></th>
                <td></td>
            </tr>
            <?php
            }
            ?>
        </table>
    </div>
</div>
<input type="submit" value="Continue to checkout" class="btn">






</body>
</html>

1 个答案:

答案 0 :(得分:0)

查看第114行,您在表单操作中在$row["id"]之间有一个空格。这很可能不会将预期的信息传递给您的“功能”。 -第116和117行在您的php代码中不包含回显,也没有使用速记回显<?='$row['pname']?>。这2个不会引起错误,但不会显示您想要的数据。