Alexhoward Posted October 16, 2008 Share Posted October 16, 2008 Hello again, i've had this issue running around for a while. my basket works fine, adding, removing, empting, everythings fine. except when you just view it navigating with: basket.php it removes everything! to add, empty, remove items i use basket.php?add=1, or basket.php?remove ans so on could some one please tell me why this is happening...? thanks in advance!! <?php include ("config.php"); //connect to the mysql server $link = mysql_connect($host, $db, $pass) or die("Could not connect to mysql because " . mysql_error()); //select the database mysql_select_db($db) or die("Could not select database because " . mysql_error()); // I replaced your two if statements with these: $product_id = isset ($_GET['id']) ? $_GET['id'] : " "; $action = isset ($_GET['action']) ? $_GET['action'] : " "; function productExists($product_id) { $sql = sprintf("SELECT id FROM products "); // You're missing stuff in your query here, specifically a WHERE clause return mysql_num_rows(mysql_query($sql)) > 0; } if (!productExists($product_id)) { die("Error. Product Doesn't Exist"); } if (!isset ($_SESSION['a'])) { $_SESSION['a'] = array (); } if (($action == "add" || $action == "remove") && !isset ($_SESSION['a'][$product_id])) { $_SESSION['a'][$product_id] = 0; } if ($action == "add") { $_SESSION['a'][$product_id]++; } elseif ($action == "remove") { $_SESSION['a'][$product_id]--; } elseif ($action = "empty") { $_SESSION['a'] = array (); } if (isset($_SESSION['a'][$product_id])&&@$_SESSION['a'][$product_id] == 0) { unset ($_SESSION['a'][$product_id]); } foreach ($_SESSION['a'] as $product_id => $quantity) { $sql = sprintf("SELECT name, description, price, pandp FROM products WHERE id = %d;", $product_id); $result = mysql_query($sql); if (mysql_num_rows($result) > 0) { list ($name, $description, $price, $pandp) = mysql_fetch_row($result); $line_cost = $price * $quantity; if (!isset ($total)) { $total = 0; } $total = $line_cost + $total; if (!isset ($post)) { $post = 0; } $post = $pandp; print " <tr> <td width='66%'><span class='style2'>$name</span></td> <td width='15%' align='center' valign='middle'><span class='style2'><a href='basket.php?action=remove&id=$product_id'><img src='images/minus.jpg' alt='Remove' width='15' height='15' border='0' valign='middle' /></a> <input type='text' value='$quantity' style='width:30' /> <a href='basket.php?action=add&id=$product_id'><img src='images/plus.jpg' alt='Add' width='15' height='15' border='0' valign='middle' /></a></span></td> <td width='17%' align='right'><span class='style2'>£ $line_cost</span></td> </tr>"; } } if (!isset($_SESSION['a'][$product_id])&&@$_SESSION['a'][$product_id] == 0) { $total = 0.00; print " <tr> <td width='66%'><span class='style2'>Your Basket is Empty...</span></td> <td width='15%' align='center' valign='middle'><span class='style2'>0</span></td> <td width='17%' align='right'><span class='style2'>0.00</span></td> </tr>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/128747-why-does-my-basket-empty-when-you-view-it/ Share on other sites More sharing options...
waynew Posted October 16, 2008 Share Posted October 16, 2008 Now when you say that it removes everything, do you mean that it actually removes everything or that the items aren't showing? Quote Link to comment https://forums.phpfreaks.com/topic/128747-why-does-my-basket-empty-when-you-view-it/#findComment-667294 Share on other sites More sharing options...
Alexhoward Posted October 16, 2008 Author Share Posted October 16, 2008 hi waynewex, thanks for the reply! it unsets the session, or set's it to zero, and displays "your basket is empty..." Quote Link to comment https://forums.phpfreaks.com/topic/128747-why-does-my-basket-empty-when-you-view-it/#findComment-667311 Share on other sites More sharing options...
DarkWater Posted October 16, 2008 Share Posted October 16, 2008 I see no session_start() on the page. >_> Quote Link to comment https://forums.phpfreaks.com/topic/128747-why-does-my-basket-empty-when-you-view-it/#findComment-667401 Share on other sites More sharing options...
Alexhoward Posted October 16, 2008 Author Share Posted October 16, 2008 Sorry, It's right at the start before a load of HTML <?php session_start(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/128747-why-does-my-basket-empty-when-you-view-it/#findComment-667416 Share on other sites More sharing options...
DeanWhitehouse Posted October 16, 2008 Share Posted October 16, 2008 put echo session_id(); and if it doesnt print anything out , then sessions arn't started. Quote Link to comment https://forums.phpfreaks.com/topic/128747-why-does-my-basket-empty-when-you-view-it/#findComment-667431 Share on other sites More sharing options...
DarkWater Posted October 16, 2008 Share Posted October 16, 2008 put echo session_id(); and if it doesnt print anything out , then sessions arn't started. You'd be better off doing: echo '<pre>' . print_r($_SESSION, true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/128747-why-does-my-basket-empty-when-you-view-it/#findComment-667434 Share on other sites More sharing options...
DeanWhitehouse Posted October 16, 2008 Share Posted October 16, 2008 It doesn't really matter what way? As they both print out something if there is a session started. Quote Link to comment https://forums.phpfreaks.com/topic/128747-why-does-my-basket-empty-when-you-view-it/#findComment-667436 Share on other sites More sharing options...
DarkWater Posted October 16, 2008 Share Posted October 16, 2008 It doesn't really matter what way? As they both print out something if there is a session started. But we can see the session data with print_r(). I don't think there's a problem with starting the session, just with using the data. Quote Link to comment https://forums.phpfreaks.com/topic/128747-why-does-my-basket-empty-when-you-view-it/#findComment-667441 Share on other sites More sharing options...
DeanWhitehouse Posted October 16, 2008 Share Posted October 16, 2008 Ok, have you also tried any error reporting error_reporting(E_ALL); ini_set('display_errors','On'); ? And echoing the session vars to check if they are correct? Quote Link to comment https://forums.phpfreaks.com/topic/128747-why-does-my-basket-empty-when-you-view-it/#findComment-667446 Share on other sites More sharing options...
Alexhoward Posted October 16, 2008 Author Share Posted October 16, 2008 Hi Guys, when i view the basket and it clears everything : using : echo session_id(); i get : ue9a0ops7javdrlr44fp5tkfl5 echo '<pre>' . print_r($_SESSION, true) . '</pre>'; i get : Array ( [a] => Array ( ) ) and i'm not sure what's ment by : error_reporting(E_ALL); ini_set('display_errors','On'); ? And echoing the session vars to check if they are correct? i'm still learning.... Quote Link to comment https://forums.phpfreaks.com/topic/128747-why-does-my-basket-empty-when-you-view-it/#findComment-667458 Share on other sites More sharing options...
DarkWater Posted October 16, 2008 Share Posted October 16, 2008 This block is suspicious: if (!isset ($_SESSION['a'])) { $_SESSION['a'] = array (); } Quote Link to comment https://forums.phpfreaks.com/topic/128747-why-does-my-basket-empty-when-you-view-it/#findComment-667471 Share on other sites More sharing options...
Alexhoward Posted October 16, 2008 Author Share Posted October 16, 2008 but the session should be set, so why would that be impacting it...? i got alot of help from here for this on too, so i'm not too sure what some bits are actually doing... cheers Quote Link to comment https://forums.phpfreaks.com/topic/128747-why-does-my-basket-empty-when-you-view-it/#findComment-667475 Share on other sites More sharing options...
DarkWater Posted October 16, 2008 Share Posted October 16, 2008 but the session should be set, so why would that be impacting it...? i got alot of help from here for this on too, so i'm not too sure what some bits are actually doing... cheers I'd suggest learning what every single part of your code does before trying to deploy it. Quote Link to comment https://forums.phpfreaks.com/topic/128747-why-does-my-basket-empty-when-you-view-it/#findComment-667476 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.