Notice: Undefined index php error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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']); }

11th Mar 2018, 10:48 AM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar
3 Answers
+ 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
11th Mar 2018, 11:15 AM
Ipang
+ 2
Thank you
12th Mar 2018, 6:53 PM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar
+ 1
You're welcome : )
12th Mar 2018, 6:54 PM
Ipang