0

On my cart page I am having an issue where the product items do not show on the web page, and if I click the remove item button, I am given the two errors that I have made the title of this post. What can I do to get past these errors?

my webpage looks like this, current webpage whereas it should look like this https://i.stack.imgur.com/Xp0xw.png, below is the code for the page.

<?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>
drino25
  • 1
  • 1
  • They do, you just have to read and understand them first. Which is line 47? – Qirel Mar 28 '19 at 13:38
  • And show us why that does not apply on your code. Show use the array that is causing the issue. – Andreas Mar 28 '19 at 13:39
  • You didn't provide the full error message, so it makes it hard to provide full information on what's wrong. But the link provided should help with the undefined index error. – aynber Mar 28 '19 at 13:39
  • @Qirel I can only see one foreach() it's in the if part just above the html – Andreas Mar 28 '19 at 13:40
  • Fair enough, @Andreas! – Qirel Mar 28 '19 at 13:42
  • @Qirel I have added a comment next to the code that throws the error, it is just above the HTML – drino25 Mar 28 '19 at 13:42
  • You just need one `if` before your `foreach` then, which should be `if (isset($_GET["action"]) && $_GET["action"] == 'delete' && !empty($_SESSION["cart"])) {` - no need to chain `if` conditions if they don't have any other sub-conditions – Qirel Mar 28 '19 at 13:43

1 Answers1

0

Looking at line 114, you have a space between $row and ["id"] in your form action. This is likely not passing expected information to your "functions". -- Rows 116 and 117 do not contain an echo in your php code, nor do they use the shorthand echo <?='$row['pname']?>. These 2 will not cause an error, but will not display your intended data.

dotParx
  • 478
  • 3
  • 8