Jump to content

Recommended Posts

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>";
  }
?>

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.

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....

 

 

 

 

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.