Here's how the code is supposed to work:
The user clicks on an 'add item' link in the collections page. That link generates the following URL:
Code: Select all
<a href="cart.php?add_item&piecenum=<?php echo $row_rs_pieceinfoї'piecenum']; ?>&qty=1">Add Item</a></td>Code: Select all
<?php
require_once('Connections/conn_my_db.php');?>
<?php
error_reporting (E_ALL);
include ("db.php");
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()
{
$hostname = 'localhost';
$username ='abc';
$password ='123';
$dbName = 'my_db';
MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect");
@MYSQL_SELECT_DB("$dbName") OR DIE("Unable to select database");
$ShowCart = mysql_query("SELECT * FROM cart INNER JOIN pieces ON cart.piecenum=pieces.piecenum WHERE cookieID='" . GetCartId() . "'");
$row_ShowCart = mysql_fetch_assoc($ShowCart);
while($row_ShowCart = mysql_fetch_array($ShowCart))
{
echo "\n<tr>";
echo "\n<td>";
echo "\n</td>";
echo "\n<td>";
echo ($row_ShowCart['description']);
echo "\n</td>";
echo "\n<td>";
echo ($row_ShowCart['price']);
echo "\n</td>";
echo "\n<td>";
echo "<a href=cart.php?action=remove_item&piecenum=",($row_ShowCart['piecenum']),">Remove</a>";
echo "\n</td>";
}
}
?>Code: Select all
<?php
// This page contains the connection routine for the
// database as well as getting the ID of the cart, etc.
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_conn_my_db = "localhost";
$database_conn_my_db = "my_db";
$username_conn_my_db = "abc";
$password_conn_my_db = "123";
$conn_fjdesigns_db = mysql_pconnect($hostname_conn_my_db, $username_conn_my_db, $password_conn_my_db) or trigger_error(mysql_error(),E_USER_ERROR);
session_start();
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
setcookie("cartID", session_id(), time() + ((3600 * 24) * 30));
return session_id();
}
}
?>Code: Select all
<?php
ob_start();
include ("include.php");
error_reporting (E_ALL);
?>Code: Select all
<?php
ShowCart();
?>Any help would be greatly appreciated. This shopping cart function has become my nemesis!