shyam13 Posted July 20, 2012 Share Posted July 20, 2012 Does using the UNSET Function on my shopping cart, destroy my $_SESSION['cart'] ? Thank You Shyam Quote Link to comment https://forums.phpfreaks.com/topic/265991-unset-function/ Share on other sites More sharing options...
xyph Posted July 20, 2012 Share Posted July 20, 2012 It could potentially. It depends on the arguments you've given it. Quote Link to comment https://forums.phpfreaks.com/topic/265991-unset-function/#findComment-1363011 Share on other sites More sharing options...
shyam13 Posted July 20, 2012 Author Share Posted July 20, 2012 This is my code: <?php if (isset($_POST['clear'])) unset($_SESSION['cart']) ?> the statement clears the cart, but when I now try and add products to the cart, nothing is getting added. Thank You Shyam Quote Link to comment https://forums.phpfreaks.com/topic/265991-unset-function/#findComment-1363012 Share on other sites More sharing options...
xyph Posted July 20, 2012 Share Posted July 20, 2012 That's odd. Check your logic, and verify that code isn't executing on every load <?php session_start(); $_SESSION['cart'] = 'test'; echo $_SESSION['cart']; // outputs 'test' unset($_SESSION['cart']); echo $_SESSION['cart']; // undefined index notice $_SESSION['cart'] = 'foobar'; echo $_SESSION['cart']; // outputs foobar ?> That proves even if you unset a variable, you should be able to redefine it afterwards. You might have errors that aren't showing as well, you could try emptying rather than unsetting if (isset($_POST['clear'])) $_SESSION['cart'] = array(); Quote Link to comment https://forums.phpfreaks.com/topic/265991-unset-function/#findComment-1363015 Share on other sites More sharing options...
shyam13 Posted July 20, 2012 Author Share Posted July 20, 2012 I have just reread my code, there is nothing wrong with my logic which is quite strange, i changed the code to use : <?php if (isset($_POST['clear'])) $_SESSION['cart'] = array(); ?> but I still have the same problem, no products are added to the cart. Thank you Shyam Quote Link to comment https://forums.phpfreaks.com/topic/265991-unset-function/#findComment-1363018 Share on other sites More sharing options...
xyph Posted July 20, 2012 Share Posted July 20, 2012 Then there's something wrong with your logic. Quote Link to comment https://forums.phpfreaks.com/topic/265991-unset-function/#findComment-1363019 Share on other sites More sharing options...
shyam13 Posted July 20, 2012 Author Share Posted July 20, 2012 I will have a look again, if the unset session has destroyed, would it be better to create a new session? Thank You Shyam Quote Link to comment https://forums.phpfreaks.com/topic/265991-unset-function/#findComment-1363022 Share on other sites More sharing options...
xyph Posted July 20, 2012 Share Posted July 20, 2012 You're destroying data within the session, not the session itself. You should easily be able to add data to it afterwards, as I showed above. Quote Link to comment https://forums.phpfreaks.com/topic/265991-unset-function/#findComment-1363023 Share on other sites More sharing options...
shyam13 Posted July 20, 2012 Author Share Posted July 20, 2012 I checked the logic and there seems to be a slight problem, which has now been fixed and I now able to add products to the cart. Thank you for your help Appreciate it Shyam Quote Link to comment https://forums.phpfreaks.com/topic/265991-unset-function/#findComment-1363025 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.