Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8817535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:52:20+00:00 2026-06-14T04:52:20+00:00

I am adding items to a cart using URL ?action=add . I have not

  • 0

I am adding items to a cart using URL ?action=add. I have not initialised the variable $count and am getting the following errors:

Notice: Undefined index: count in C:\wamp\www\cart.php on line 31

Notice: Undefined index: count in C:\wamp\www\cart.php on line 32

I know I need to define the $count variable. ($count = 0;) But I dont want the page to refresh and have the SESSION variable reset to 0 every time a user adds a new item to the cart. The $count needs to be used as a counter on the next page. How can I do this?

Here is my case code:

switch($action) { 
    case "add":
        if (isset($_SESSION['cart'][$comic_id])) {
            $count=$_SESSION['count']; //LINE 31
            $_SESSION['count']++; //LINE 32
            $_SESSION['cart'][$comic_id]++;

        } else {
            $_SESSION['cart'][$comic_id] = 1;
            }
    break;  

After adding if (!isset($_SESSION[‘count’])) $_SESSION[‘count’] = 1;

<?php 
if(!isset($_SESSION)) {
     session_start();
}
$total = 0;
$page = 'cart';

$dbname = "crystal_fusion";
$dbhost = "127.0.0.1";
$dbuser = "root";
$dbpwd = "";

$link = mysql_connect($dbhost, $dbuser, $dbpwd);

mysql_select_db($dbname);

if(isset($_GET['comic_id'])){
    $comic_id = $_GET['comic_id'];
} else {
    $comic_id = NULL;}

$action = $_GET['action'];

if($comic_id && !productExists($comic_id)) {
    die("We apologise, but we can't seem to find the comic you were looking for!!!");
}

if (!isset($_SESSION['count'])) $_SESSION['count'] = 1; //added as suggested - now output is correct when adding multiples of 1 item. However, the count is wrong if more than 1 item is added.

switch($action) {
    case "add":
        if (isset($_SESSION['cart'][$comic_id])) {
            $count=$_SESSION['count'];
            $_SESSION['count']++;
            $_SESSION['cart'][$comic_id]++;

        } else {
            $_SESSION['cart'][$comic_id] = 1;
            }
    break;  
    case "remove":
        if (isset($_SESSION['cart'][$comic_id])) {
            $_SESSION['cart'][$comic_id]--;} 
        if($_SESSION['cart'][$comic_id] == 0) unset($_SESSION['cart'][$comic_id]); 
    break;  
    case "empty":
        unset($_SESSION['cart']); 
        session_destroy();
    break;  
}

require('header.php');
require('sidebar.php');
if (isset($_SESSION['cart'][$comic_id])){

    echo "<table border=\"0\" padding=\"10\" width=\"80%\">";
    echo "<td colspan=\"1\" align=\"left\"><a href=\"title.php\">Continue Shopping</a></div>";
    echo "<td colspan=\"6\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Crystal Fusion: Are you sure you wish to empty your cart?');\">Empty Cart</a></td>"; //empty cart link --Sam                      
    echo "<tr height=\"20px\">";
    echo "<tr height=\"20px\">";
    echo "<td align=center>Image</td><td align=center>Title</td><td align=center>Description</td><td colspan=3 align=center>Copies (+/-)</td><td align=center>Price</td>";
    echo "<tr height=\"20px\">";


    foreach($_SESSION['cart'] as $comic_id => $qty) {   

        $sql = sprintf("SELECT title, description, cost, image_thumbnail
                FROM comic 
                WHERE comic_id = %d;",$comic_id);

        $result = mysql_query($sql);

        if(mysql_num_rows($result) > 0) {

            list($name, $description, $price, $image_thumbnail) = mysql_fetch_row($result);



            $cost = $price * $qty;  
            $total = $total + $cost; 

            $cost = number_format($cost,2);
            $total = number_format($total,2);
            $description =  substr($description, 0, 250);

            echo "<br><tr>";
            echo "<td width=\"10px\" align=\"center\"><img height=100 align=center src=\"$image_thumbnail\">";
            echo "<td align=\"center\">$name</td>";
            echo "<td width=\"40%\" align=\"center\">$description...<a href=comic_dyn.php?comic_id=$comic_id>More Info</td>";
            echo "<td width=\"30px\" align=\"center\"><a href=\"$_SERVER[PHP_SELF]?action=add&comic_id=$comic_id\">+<br></a><td align=\"center\">$qty <td width=\"20px\" align=\"center\"><a href=\"$_SERVER[PHP_SELF]?action=remove&comic_id=$comic_id\">-</a></td>";
            echo "<td align=\"right\">$$cost</td>";
            echo "</tr>";           
        }

    }

    echo "<br><tr><tr height=100px>";
    echo "<td><td><td colspan=\"4\" align=\"right\">Total:</td>";
    echo "<td width=\"60px\" align=\"right\">$$total</td>";
    echo "<tr><td colspan=\"7\" align=\"right\"><a href=\"checkout_html.php\">Proceed to Checkout</a>";
    echo "<tr height=\"50px\">";
    echo "</table>";
}else{

    echo "Your cart is currently empty.";
    echo "<br><br><td colspan=\"1\" align=\"left\"><a href=\"title.php\">Continue Shopping</a></div>";

}

$_SESSION['totnamqty'][]=$name . " " . $qty . " " . $price;

function productExists($comic_id) { 
        $sql = sprintf("SELECT * FROM comic WHERE comic_id = %d;", $comic_id);
    return mysql_num_rows(mysql_query($sql)) > 0;
    }
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:52:22+00:00Added an answer on June 14, 2026 at 4:52 am

    Before using somewhere $_SESSION['count'] you have to initialize it. i think you didn’t done it and that’s why you are getting the notices.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

so I have a problem with dynamically adding items to menuStrip. I mean I
I'm adding a series of buttons to a UINavigationBar using: NSArray *items; items =
Now I can add items with store.newItem() . After adding it, it's placing it
I have a cart which contains items, in my controller index method I use
Using following method to create Shopping Cart Session http://net.tutsplus.com/tutorials/other/build-a-shopping-cart-in-aspnet/ Now running locally on my
I'm testing adding items to the shopping cart like this: $item = $this->model->getSingleItem(); for($i
I have a product page with an add to cart button When the add
I wrote a functional test to check adding items to a shopping cart.For a
Im adding items to TreeView control via ItemsSource property and ItemTemplate property to set
In my Phonegap android application dynamically adding list items to jqm listview where on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.