I'm having some problems with the following code - I get a couple of errors:
The 'action' is set when the user adds an item to the cart from a collections page. I'm not sure whether or not this can be set as a default on the page somewhere.Notice: Undefined index: action in cart.php on line 26
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at cart.php:26) in cart.php on line 16
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at cart.php:26) in cart.php 16
Warning: Cannot modify header information - headers already sent by (output started at cart.php:26) in cart.php on line 17
Regarding setting the cookies - would this be better to be in an include file somewhere? Is it getting called repeatedly thus the error?
Thanks!
Here is the code (the WHOLE page!):
Code: Select all
<?php
function GetCartId()
{
// This function will generate an encrypted string and
// will set it as a cookie using set_cookie. This will
// also be used as the cookieId field in the cart table
if(isset($_COOKIE["cartID"]))
{
return $_COOKIE["cartID"];
}
else
{
// There is no cookie set. We will set the cookie
// and return the value of the users session ID
session_start();
setcookie("cartID", session_id(), time() + ((3600 * 24) * 30));
return session_id();
}
}
require_once('Connections/conn_mydb_db.php');
error_reporting(E_ALL);
switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["piecenum"], $_GET["qty"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["piecenum"], $_GET["qty"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["piecenum"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}
function AddItem($piecenum, $qty)
{
// Will check whether or not this item
// already exists in the cart table.
// If it does, the UpdateItem function
// will be called instead
// Check if this item already exists in the users cart table
$result = mysql_query("SELECT count(*) FROM cart WHERE cookieID = '" . GetCartId() . "' AND piecenum = $piecenum");
$row = mysql_fetch_row($result);
$numRows = $row[0];
if($numRows == 0)
{
// This item doesn't exist in the users cart,
// we will add it with an insert query
@mysql_query("INSERT INTO cart(cookieID, piecenum, qty) VALUES('" . GetCartId() . "', $piecenum, $qty)");
}
else
{
// This item already exists in the users cart,
// we will update it instead
UpdateItem($piecenum, $qty);
}
}
function UpdateItem($piecenum, $qty)
{
// Updates the quantity of an item in the users cart.
// If the qutnaity is zero, then RemoveItem will be
// called instead
// Get a connection to the database
if($qty == 0)
{
// Remove the item from the users cart
RemoveItem($piecenum);
}
else
{
mysql_query("UPDATE cart SET qty = $qty WHERE cookieID = '" . GetCartId() . "' AND piecenum = $piecenum");
}
}
function RemoveItem($piecenum)
{
// Uses an SQL delete statement to remove an item from
// the users cart
mysql_query("DELETE FROM cart WHERE cookieID = '" . GetCartId() . "' AND piecenum = $piecenum");
}
function ShowCart()
{
// Gets each item from the cart table and display them in
// a tabulated format, as well as a final total for the cart
$totalCost = 0;
$result = mysql_query("SELECT * FROM cart INNER JOIN pieces ON cart.piecenum=pieces.piecenum WHERE cookieID='" . GetCartId() . "'");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Shopping Cart</title>
<script language="JavaScript">
function UpdateQty(item)
{
itemId = item.name;
newQty = item.options[item.selectedIndex].text;
document.location.href = 'cart.php?action=update_item&piecenum='+itemId+'&qty='+newQty;
}
</script>
<!-- End Preload Script -->
<style type="text/css">
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
</style>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
</head>
<body onload="preloadImages();">
<table style="height:100%" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="vertical-align:top;"><table style="height:76px;" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="background-color: rgb(102,153,163); width:125px; height:80px;"><a href="index.php"><img src="images/Logo.gif" alt=" " width="125" height="75" border="0" align="middle" /></a></td>
<td><table style="height:100%;" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="background-color: rgb(126, 127, 117); height:40px" align="center"><!-- ImageReady Slices (TopNav.psd) -->
<img
src="images/TopNav.gif" alt=""
name="TopNav_01" width="500" height="38"
border="0" align="middle" usemap="#TopNav_01_Map" id="TopNav_01" />
<map
name="TopNav_01_Map">
<area shape="rect" alt="" coords="403,6,487,31"
href="contactus.php"
onmouseover="changeImages('TopNav_01', 'images/TopNa-06.gif'); return true;"
onmouseout="changeImages('TopNav_01', 'images/TopNav_0.gif'); return true;" />
<area shape="rect" alt="" coords="293,5,376,34"
href="aboutus.php"
onmouseover="changeImages('TopNav_01', 'images/TopNa-05.gif'); return true;"
onmouseout="changeImages('TopNav_01', 'images/TopNav_0.gif'); return true;" />
<area shape="rect" alt="" coords="207,8,258,33"
href="orders.php"
onmouseover="changeImages('TopNav_01', 'images/TopNa-04.gif'); return true;"
onmouseout="changeImages('TopNav_01', 'images/TopNav_0.gif'); return true;" />
<area shape="rect" alt="" coords="86,7,169,30"
href="collections.php?categorydesc=Sets"
onmouseover="changeImages('TopNav_01', 'images/TopNa-03.gif'); return true;"
onmouseout="changeImages('TopNav_01', 'images/TopNav_0.gif'); return true;" />
<area shape="rect" alt="" coords="10,8,62,31"
href="index.php"
onmouseover="changeImages('TopNav_01', 'images/TopNa-02.gif'); return true;"
onmouseout="changeImages('TopNav_01', 'images/TopNav_0.gif'); return true;" />
</map>
<!-- End ImageReady Slices --></td>
</tr>
<tr>
<td style="background-color: rgb(221, 222, 194); height:40px;" align="center"></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td> </td>
<td> </td>
<td width="25%" align="right" valign="top"><a href="login.php"><img src="images/login.gif" border="0" /></a><img src="images/hyphen.gif" /><a href="registration.php"><img src="images/register.gif" border="0" /></a> </td>
</tr>
</table>
<tr>
<td align="center"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td></td>
</tr>
<tr>
<td align"center" width="15%" height="25"><h1>Your Shopping Cart</h1>
<form name="frmCart" method="get">
<table align="center" width="80%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="15%" height="25"><h4>Qty</h4></td>
<td width="55%" height="25"><h4>Product</h4></td>
<td width="20%" height="25"><h4>Price Each</h4></td>
<td width="10%" height="25"><h4>Remove?</h4></td>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
// Increment the total cost of all items
$totalCost += ($row["qty"] * $row["price"]);
?>
<tr>
<td width="15%" height="25"><select name="<?php echo $row["piecenum"]; ?>" onChange="UpdateQty(this)">
<?php
for($i = 1; $i <= 20; $i++)
{
echo "<option ";
if($row["qty"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>
</select>
</td>
<td width="55%" height="25"><h3> <?php echo $row["description"]; ?> </h3></td>
<td width="20%" height="25"><h3> $<?php echo number_format($row["price"], 2, ".", ","); ?> </h3></td>
<td width="10%" height="25"><a href="cart.php?action=remove_item&piecenum=<?php echo $row["piecenum"]; ?>">Remove</a> </td>
</tr>
<?php
}
// Display the total
?>
<tr>
<td width="100%" colspan="4"></td>
</tr>
<tr>
<td width="70%" colspan="2"><br />
<a href="collections.php">Keep Shopping</a> </td>
<td width="30%" colspan="2"><br />
<h4>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></h4></td>
<?php
}
?>
</tr>
</table>
</form></td>
</tr>
<tr>
<td style="height:30px;" align="center"></td>
</tr>
<tr>
<td style="background-color: rgb(126, 127, 117); height:20px;"> </td>
</tr>
<tr>
<td style="background-color: rgb(126, 127, 117); height:20px;"> </td>
</tr>
<tr>
<td style="text-align:center"><br />
<br />
<h3>Copyright © 2004 . All rights reserved. All graphics, pictures, contents, and jewelry designs are copyrighted and protected by US and international law.†Use, reproduction, or modification in any manner without written permission is prohibited.</h3></td>
</tr>
</table>
</body>
</html>