Results 1 to 8 of 8

Thread: Undefined Index

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    18

    Undefined Index

    Hi,

    I kept getting the 'undefined index' error message was passing an primary key variable when using a GET from the URL.

    to over come it I used this line of code - <?php error_reporting (E_ALL ^ E_NOTICE); ?>


    Is it wise to use this? I couldnt work out why the error message was coming up.

    Thanks
    Twitter @dancematt

    Programmers unite

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Undefined Index

    Better to fix the problem. "Undefined index" means you're trying to access an element in an array that doesn't exist. For example:
    PHP Code:
    $myArray = array('a'=>'apple','b'=>'banana');
    echo 
    $myArray['c']; //there is no such element; will produce an 'undefined index' error 
    If you're unsure whether or not the index will be set, you can use isset() to check.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Undefined Index

    Or, if you need to be able to distinguish between an item which doesn't exist and an item which exists but has a null value, use array_key_exists.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    18

    Re: Undefined Index

    Thanks for the quick replies.

    Its the $_GET['id'] that is causing the issues. What I cant figure out is where to define the variable

    I assumed that the var was defined as $product_id



    PHP Code:
    <?php

    $product_id 
    $_GET['id']; //the product id from the URL
    $action $_GET['action']; //the action from the URL

    //if there is an product_id and that product_id doesn't exist display an error message
    if($product_id && !productExists($product_id)) {
        die(
    "Error. Product Doesn't Exist");
    }

    switch(
    $action) { //decide what to do

        
    case "add":
            
    $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id
        
    break;

        case 
    "remove":
            
    $_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id
            
    if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items.
        
    break;

        case 
    "empty":
            unset(
    $_SESSION['cart']); //unset the whole cart, i.e. empty the cart.
        
    break;

    }

    ?>
    Twitter @dancematt

    Programmers unite

  5. #5
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Undefined Index

    As said, you'll need to check that $_GET['id'] is set before using it (assigning its value to another variable counts as "using it").
    PHP Code:
    $product_id = isset($_GET['id']) ? $_GET['id'] : 0//the product id from the URL 
    Are you familiar with the ternary operator?

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    18

    Re: Undefined Index

    Thanks for the replys. I wasnt sure what a ternary operator was but I follow now.

    Would some please look at the code ive attached, it seems all over the place. When theres only one item in the cart I get an error message and then when the car is empty I get an error message.

    Thanks



    cart.php

    Products.php
    Twitter @dancematt

    Programmers unite

  7. #7
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Undefined Index

    What is the error message you're getting? On which page (with what query string)?

  8. #8
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Undefined Index

    Some things that I found:

    - When the user removes an item, you are decrementing the session variable. And if it reaches 0, you are unsetting that session variable. Instead, you could check whether it is 0. Then, do not decrement it. Otherwise, decrement it.

    - At the bottom of the page, you have this line:
    Code:
    </html>//www.w3.org/1999/xhtml">
    That kind of commenting won't work as you are doing it over the HTML code. That commenting is possible for PHP code only, not for HTML !
    For HTML, you have to comment it like this:
    Code:
    <!--
      comment goes here...
    -->
    - To view cart, you are linking directly to the cart.php page. But in that page, you are assigning "action" from the GET variable. But you are not taking care of the case in which it is not set. ie, to view cart page, you are simply linking to "cart.php", passing no values in GET method. So, that would endup in an exception.

    These are some of things that I found while having a quick look at the code.


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width