MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 19th, 2004, 10:28 AM
negar negar is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 1 negar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Building A Persistent Shopping Cart With PHP and MySQL

i used this tutorial to build a shopping cart. however when i click on the add item button i get to the shopping cart page but the item does not get added. i have my items table is called Products and its attributes are ProdID (primary key), Product, Prodname, Prodtype, Supplier, Price.
here is my cart.php code
PHP Code:
<?php
include("db.php");
 
switch(
$_GET["action"])
{
case 
"add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case 
"update_item":
{
UpdateItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case 
"remove_item":
{
RemoveItem($_GET["id"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}
function 
AddItem($ProdID$qty)
{
// Will check whether or not this item
// already exists in the cart table.
// If it does, the UpdateItem function
// will be called instead
 
//global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
//$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
 
// Check if this item already exists in the users cart table
$result mysql_query("select count(*) from cart where cookieID = '" GetCartID() . "' and ProdID = $ProdID");
$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, ProdID, qty) values('" GetCartID() . "', $ProdID, $qty)");
}
else
{
// This item already exists in the users cart,
// we will update it instead
 
UpdateItem($ProdID$qty);
}
}
 
function 
UpdateItem($ProdID$qty)
{
// Updates the quantity of an item in the users cart.
// If the qutnaity is zero, then RemoveItem will be
// called instead
//global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
//$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
 
if($qty == 0)
{
// Remove the item from the users cart
RemoveItem($ProdID);
}
else
{
mysql_query("update cart set qty = $qty where cookieID = '" GetCartID() . "' and ProdID = $ProdID");
}
}
 
function 
RemoveItem($ProdID)
{
// Uses an SQL delete statement to remove an item from
// the users cart
//global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
//$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
 
mysql_query("delete from cart where cookieID = '" GetCartID() . "' and ProdID = $ProdID");
}
 
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
 
//global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
//$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
 
$totalCost 0;
 
$result mysql_query("select * from cart inner join Products on cart.ProdID = Products.ProdID where cart.cookieID = '" GetCartID() . "' order by Products.Prodname asc");
?>
<html>
<head>
<title> Your Shopping Cart </title>
<script language="JavaScript">
 
function UpdateQty(item)
{
ProdID = item.name;
newQty = item.options[item.selectedIndex].text;
 
document.location.href = 'cart.php?action=update_item&id='+ProdID+'&qty='+newQty;
}
 
</script>
</head>
<body bgcolor="#ffffff">
<h1>Your Shopping Cart</h1>
<form name="frmCart" method="get">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="15%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
&nbsp;&nbsp;<b>Qty</b>
</font>
</td>
<td width="55%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Product</b>
</font>
</td>
<td width="20%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Price Each</b>
</font>
</td>
<td width="10%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Remove?</b>
</font>
</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">
<font face="verdana" size="1" color="black">
<select name="<?php echo $row["ProdID"]; ?>" onChange="UpdateQty(this)">
<?php
 
for($i 1$i <= 20$i++)
{
echo 
"<option ";
if(
$row["qty"] == $i)
{
echo 
" SELECTED ";
}
echo 
">" $i "</option>";
}
?>
</select>
</font>
</td>
<td width="55%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["Prodname"]; ?>
</font>
</td>
<td width="20%" height="25">
<font face="verdana" size="1" color="black">
$<?php echo number_format($row["Price"], 2"."","); ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="1" color="black">
<a href="cart.php?action=remove_item&id=<?php echo $row["ProdID"]; ?>">Remove</a>
</font>
</td>
</tr>
<?php
}
 
// Display the total
?>
<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<tr>
<td width="70%" colspan="2">
<font face="verdana" size="1" color="black">
<a href="order2.php"><< Keep Shopping</a>
</font>
</td>
<td width="30%" colspan="2">
<font face="verdana" size="2" color="black">
<b>Total: $<?php echo number_format($totalCost2"."","); ?></b>
</font>
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
}
?>

can anyone help?
Thanx

Last edited by stumpy : March 20th, 2004 at 04:19 AM. Reason: Place your code in code tags or your post may be removed.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > Building A Persistent Shopping Cart With PHP and MySQL


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT