0
Notice: Undefined index php error
Error i am recieving: Notice: Undefined index: remove in C:\xampp\htdocs\shopp\shopping-cart.php on line 7 Important part of my code: session_start(); //remove products from cart if(!isset($_GET['remove'])) foreach($_SESSION['cart'] as $key => $val) { if($val==$_GET['remove']) //this is line 7 { unset($_SESSION['cart'][$key]); } } elseif(isset($_GET['remove_all'])){ unset($_SESSION['cart']); }
3 Réponses
+ 2
Okay, let's review this first...
session_start();
// You need to wrap the foreach block in { }
// because it contains more than one line
// of instructions
if(!isset($_GET['remove']))
{
foreach($_SESSION['cart'] as $key => $val)
{
// here you are comparing $val against
// a non-existent value ($_GET['remove'])
// the if(!isset($_GET['remove'])) above
// had verified that. There is no value
// in $_GET having key 'remove'. You can
// change it to if(isset($_GET['remove']))
// to be sure we have a valid value to be
// compared with $val here.
if($val==$_GET['remove']) //this is line 7
{
unset($_SESSION['cart'][$key]);
}
}
}
elseif(isset($_GET['remove_all'])){
unset($_SESSION['cart']);
}
Hth, cmiiw
+ 2
Thank you
+ 1
You're welcome : )