Hi all,
I've currently got this problem with my sopping cart were if i test it on my local server it works fine but not on the remote server.
I would appreciate someone taking a look at the site.
http://dvdrealm.com.au/products.php?cat=2
If you click on the "BUY" button it successfully adds the item to the cart. Now, if you go back (or "Continue Shopping") and add another item the session does not appear to update and display the new item to the cart. You will notice that the "Subtotal" is correct.
I can't understand why it is working on my local server (same PHP version as remote server) but not the remote server.
This is the code for the "BUY" button:
<form action="vsadmin/addtocart.php" method="post">
<input type="image" src="images/buy.gif" width="44" height="24" border="0">
<input name="movie_id" type="hidden" value="<? echo $row1["movie_id"] ?>">
</form>
The addtocart.php script is:
<?php
ob_start();
include ('functions.php');
session_start();
if (!empty($_POST['movie_id'])) {
// Really should do some checking here because otherwise people can just
// enter stuff in from the url and cause no end of problems
$movie_id = $_POST['movie_id'];
// Starting a new cart session
if (!session_is_registered("cart")) {
$_SESSION['cart'] = array();
$_SESSION['items'] = 0;
$_SESSION['total_price'] = 0.00;
}
// Existing DVD in the cart + 1
if (!isset($_SESSION['cart'][$movie_id])) {
$_SESSION['cart'][$movie_id]++;
}
else {
$_SESSION['cart'][$movie_id] = 1;
}
$_SESSION['total_price'] = calculate_price($_SESSION['cart']);
$_SESSION['items'] = calculate_items ($_SESSION['cart']);
header ("Location: ../cart.php");
}
ob_end_flush();
?>
...and the include file (functions.php) looks like:
<?PHP
function calculate_price($cart)
{
$_SESSION['price']=0.0;
if(is_array($_SESSION['cart']))
{
$connection=mysql_connect("localhost","*","*") or die('Could not connect to the database server');
$db = mysql_select_db("dvdrealm", $connection) or die ("Unable to select database.");
foreach($_SESSION['cart'] as $isbn => $qty)
{
$sql= "SELECT
movie_current_price
FROM
movie
WHERE
movie_id='$isbn'";
$sql_result = mysql_query($sql,$connection) or die ("Could not select data");
while ($row = mysql_fetch_row($sql_result))
{
$_SESSION['movie_current_price']= $row['0'];
}
$_SESSION['price'] += $_SESSION['movie_current_price']*$qty;
}
}
return $_SESSION['price'];
}
function calculate_items($cart)
{
$_SESSION['items']=0;
if(is_array($_SESSION['cart']))
{
foreach($_SESSION['cart'] as $isbn => $qty)
{
$_SESSION['items'] += $qty;
}
}
return $_SESSION['items'];
}
?>
when the addtocart.php script does its thing it goes to cart.php.
This is were it is not displaying the updated cart items.
The cart.php script is:
<?
session_start();
?>
include ('functions.php');
$connection=mysql_connect("localhost","","") or die('Could not connect to the database server');
$db = mysql_select_db("dvdrealm", $connection) or die ("Unable to select database.");
$i=0;
if(!session_is_registered("cart") OR count($_SESSION['cart'])==0)
{
echo "Your cart is empty";
} elseif(session_is_registered("cart") AND count($_SESSION['cart'])!=0)
{
foreach ($_SESSION['cart'] as $isbn => $qty)
{
$total_price = calculate_price($_SESSION['cart']);
$items =calculate_items($_SESSION['cart']);
$sql = "select * from movie where movie_id='$isbn' group by movie_title order by movie_title asc";
$sql_result = mysql_query($sql,$connection) or die ("Could not select data");
while ($row = mysql_fetch_array($sql_result))
{
$movie_title = stripslashes($row['movie_title']);
$movie_current_price = $row['movie_current_price'];
}
?>
RESULTS HERE
<?php
$i += 1;
}
}
If anyone has any ideas as to what the problem is it would be most appreciated.
Cheers,
micmac