0

I have set up a shopping cart system to simply add something to a cart (no purchasing is required). This is my cart.php code

<?php

session_start();

include 'connection.php';
include 'item.php';

$sql = "SELECT * FROM apps";
$result = mysqli_query($con, $sql);
$product = mysqli_fetch_object($result);

if(isset($_GET['AppName'])) {
    $item = new Item();
    $item->AppName = $product->AppName;
    $item->AndroidPrice = $product->AndroidPrice;
    $item->iPhonePrice = $product->iPhonePrice;
    $item->quantity = 1;
    $_SESSION['cart'][] = $item;
}
?>

<table cellpadding="2" cellspacing="2" border="1">
    <tr>
        <th>AppName</th>
        <th>AndroidPrice</th>
        <th>iPhonePrice</th>
        <th>quantity</th>
        <th>Sub Total</th>
    </tr>
    <?php
        $cart = unserialize(serialize($_SESSION['cart']));
        for ($i=0; $i<count($cart); $i++) 
    ?>
    <tr>
        <td><?php echo $cart[$i]->AppName; ?></td>
        <td><?php echo $cart[$i]->AndroidPrice; ?></td>
        <td><?php echo $cart[$i]->iPhonePrice; ?></td>
        <td><?php echo $cart[$i]->quantity; ?></td>
        <td><?php echo $cart[$i]->price * $cart[$i]->quantity; ?></td>
    </tr>
</table>

The error I receive is Notice: Undefined index: cart in E:\xampp\htdocs\cart.php on line 31.

Line 31 is $cart = unserialize(serialize($_SESSION['cart'])); - What am I doing wrong?

A little more info if needed. my item.php:

<?php

class item{
    var $AppName;
    var $AndroidPrice;
    var $iPhonePrice;
    var $quantity;
}

?>

and my app page that should add items to cart.

<?php
include 'connection.php';

$sql = "SELECT * FROM apps";
$result = mysqli_query($con, $sql);
?>
<table cellpadding="2" cellspacing="2" border="0">
    <tr>
        <th>AppName</th>
        <th>AndroidPrice</th>
        <th>iPhonePrice</th>
        <th>Description</th>
        <th>Noofdownloads</th>
        <th>Buy</th>
    </tr>
<?php   
        while ($product = mysqli_fetch_object($result)){ ?>
        <tr>
            <td><?php echo $product->AppName; ?></td>
            <td><?php echo $product->AndroidPrice; ?></td>
            <td><?php echo $product->iPhonePrice; ?></td>
            <td><?php echo $product->Description; ?></td>
            <td><?php echo $product->Noofdownloads ?></rd>
            <td><a href="cart.php">Add to Cart</a></td>
        </tr>
    <?php } ?>
</table>

Will appreciate any help at all. I am pretty bad at PHP and a beginner so help me out D:

Naim
  • 1
  • 1
    That warning means `$_SESSION['cart']` does not exist. I can see it's being set only if `if ( isset($_GET['AppName']) )` is satisfied. Is `$_GET['AppName']` set too? Would typically be from a URL, eg. `http://example.com?AppName` That's the only way `$_SESSION['cart']` is going to get set, per your code. – mferly Apr 20 '16 at 20:09
  • And `unserialize(serialize($_SESSION['cart']))` is crazy redundant lol. You're `serialize()`'ing it only to immediately `unserialize()` it right after. – mferly Apr 20 '16 at 20:10

0 Answers0