-
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
-
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.
-
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.
-
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;
}
?>
-
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?
-
2 Attachment(s)
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 :confused:
Attachment 87544
Attachment 87545
-
Re: Undefined Index
What is the error message you're getting? On which page (with what query string)?
-
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.
:wave: