I have been messing with this script all day. I took a look at my database and realized that I don't need to pass the video_id since I can link back to it with price_id. See below:
DATABASE STRUCTURE
Table Video (VideoId, VideoTitle, VideoFilename, VideoPhoto, VideoThumbnail, VideoDescription, VideoBodypart, VideoTimestamp, VideoDelete)
Price (PriceId, PriceVideoId, PriceAmount, PriceTimeId, PriceAmount)
Time (TimeId, TimeTitle, TimeLength)
Those are just the main tables...
Each video has its own id value (VideoId). Each price has its own price id value (PriceId) and a video id value (PriceVideoId). This is how I link the three prices (one for each purchase length 1-day, 3-days, and 7-days and each video has different prices for these values) to one video. So, then I link the time of the purchase to the price id value so that I can insert into the purchase table as to when the video expires for each customer.
I re-coded as shown below and everything works except when I add more than one video and then try to remove one of them. It empties the whole cart BUT...if I press the back button it removes the product that I wanted to remove in the first place. So, basically it works, but only when I press the back button, lol. I hope I explained this good enough. Below is the code. Please help me fix this.
<?php
// if no session is set, initiate one
if (!isset($_SESSION))
{
session_start();
}
// see if the customer is logged in
//if (!isset($_SESSION['customer_id']))
//{
//header("location:login.php");
//}
// connect to the database
require_once('connect.php');
// select the database
mysql_select_db($database, $connect);
// get the video information
//if (isset($_GET['video_id'])) {
//$video_id = $_GET['video_id'];
//} else {
//$video_id = 1;
//}
// get the action
if (isset($_GET['action'])) {
$action = $_GET['action'];
} else {
$action = "empty";
}
// get the video pricing information
if (isset($_GET['price_id'])) {
$price_id = $_GET['price_id'];
} else {
$price_id = 1;
}
// do the following code based on the action
switch($action) {
// if the action is add then add the product id to the cart session array
case "add":
if (isset($_SESSION['cart'][$price_id])) {
$_SESSION['cart'][$price_id]++;
} else {
$_SESSION['cart'][$price_id] = 1;
}
break;
// if the action is remove then remove the product id from the cart session array
case "remove":
if (isset($_SESSION['cart'][$price_id])) {
$_SESSION['cart'][$price_id]--;
if ($_SESSION['cart'][$price_id]==0) {
unset($_SESSION['cart']);
}
}
break;
// if the action is empty then remove the cart session array completely
case "empty":
unset($_SESSION['cart']);
break;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta name="Description" content="Information architecture, Web Design, Web Standards." />
<meta name="Keywords" content="your, keywords" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="layout.css" type="text/css" />
<title>PPV Workouts - Shopping Cart</title>
</head>
<body>
<!--wrap starts here-->
<div id="wrap">
<?php include('header.php'); ?>
<?php include('menu.php'); ?>
<!--content-wrap starts here-->
<div id="content-wrap" class="two-col">
<?php include('sidebar.php'); ?>
<!--main content starts here-->
<div id="main">
<h1>Shopping Cart</h1>
<br />
<?php if (isset($_SESSION['cart'])) {
$total = 0; ?>
<table width="95%" border="0" cellspacing="0" cellpadding="5">
<?php foreach($_SESSION['cart'] as $price_id => $x) {
$query = mysql_query("SELECT VideoId, VideoTitle, PriceId, PriceVideoId, PriceAmount, PriceTimeId, TimeId, TimeTitle, TimeLength FROM Video, Price, Time WHERE PriceId = $price_id AND PriceVideoId = VideoId AND PriceTimeId = TimeId", $connect);
$row = mysql_fetch_array($query);
$video_name = $row['VideoTitle'];
$order_length = $row['TimeTitle'];
$video_price = $row['PriceAmount'];
$line_cost = $video_price * $x;
$total_cost = $total + $line_cost;
?>
<tr>
<td align="left" valign="middle"><?php echo $video_name; ?></td>
<td align="center" valign="middle"><?php echo $order_length; ?></td>
<td align="center" valign="middle"><?php echo "$".$line_cost; ?></td>
<td align="center" valign="middle"><a href="cart.php?action=remove&price_id=<?php echo $price_id; ?>">Remove</a></td>
</tr>
<?php } ?>
<tr>
<td colspan="4" bgcolor="#666666"><span class="shopping_cart">Purchase Total: $<?php echo $total_cost; ?></span></td>
</tr>
</table>
<br />
<div align="center">
<form id="shopping_cart" method="post" action="confirm.php">
<input type="submit" name="place_order" id="place_order" value="Place Order" />
</form>
</div>
<div align="center">
<p><a href="search.php">Continue Shopping</a></p>
<p><a href="cart.php?action=empty">Empty Cart</a></p>
</div>
<?php }
else
{
echo "<br /><br /><div align='center'>There are no items in your cart.</div>";
} ?>
<!--main content ends here-->
</div>
<!--content-wrap ends here-->
</div>
<?php include('footer.php'); ?>
<!--wrap ends here-->
</div>
</body>
</html>