Page 1 of 1
Nothing being sent to DB
Posted: Thu Feb 20, 2003 12:52 pm
by don_assi
Hi Guys,
I am really in need of some advice or help... this file does not seem to be sending the right info to the 'shopping' table... is their an issue with $table = ("shopping"); does this need to be $_POST?
Below is the file that pulls up the items... and on clicking Add Item it should send some details to the 'shopping' table...
Code: Select all
<?
mysql_connect("hermes", "gassi01","Capone77");
mysql_select_db( "gassi01db");
$table = ("shopping");
include( "shoppingcart.php");
$cart = new Cart;
//if(!$ShoppingCart)
// $ShoppingCart = $session;
?>
<HTML>
<BODY>
<FORM ACTION="store.php" METHOD=post>
<CENTER><B><FONT SIZE="+2">Products</B></FONT></CENTER><BR><BR>
<?
$result = mysql_query( "SELECT * FROM inventory");
while($row = mysql_fetch_array($result))
{
echo "Product: $rowїproduct]<BR>";
echo "Description: $rowїdescription]<BR>";
echo "Price: \$$rowїprice]<BR>";
echo "<A HREF="view_cart.php?add=".urlencode($rowїproduct]);
if (!$ShoppingCart)
echo "&session=$session">";
else
echo "">";
echo "Add this item to your cart</A><BR><BR>";
}
echo "For documentation and source, go ";
echo "<A
HREF="http://www.montana.com/ethan/mycart/example/shop.html">here</A>\n";
?>
</FORM>
</BODY>
</HTML>
Would really appreciate your help.
Thanks
don_assi
Posted: Thu Feb 20, 2003 12:56 pm
by daven
A) why do you have parentheses "()" in the line: $table=("shopping") ?
B) Do you actually access that table anywhere in the page (I do not see a reference to it)
C) What does the code for "view_cart.php" look like (the page where the data is actually inserted)? It is difficult to figure out the problem without a reference.
Posted: Thu Feb 20, 2003 1:20 pm
by don_assi
Thanks for your time with this... the other two files are:
view_cart.php
Code: Select all
<?
mysql_connect("hermes", "gassi01","Capone77");
mysql_select_db( "gassi01db");
include( "shoppingcart.php");
$table =("shopping");
$cart = new Cart;
if($add)
{
if($ShoppingCart)
$session = $ShoppingCart;
$result = mysql_query( "SELECT * FROM inventory WHERE product='$add'");
$row = mysql_fetch_array($result);
$cart->add_item($table,$session,$rowїproduct],1);
}
if($remove)
{
if($ShoppingCart)
{
$session = $ShoppingCart;
}
$result = mysql_query( "SELECT * FROM inventory WHERE product='$remove'");
$row = mysql_fetch_array($result);
$cart->delete_item($table,$session,$rowїproduct]);
}
if($modify)
{
$contents = $cart->display_contents($table,$session);
for($i = 0; $i < sizeof($quantity); $i++)
{
$oldquan = $contentsїquantity]ї$i];
$product = $contentsїproduct]ї$i];
$newquan = $quantityї$product];
$cart->modify_quantity($table,$session,$product,$newquan);
}
}
// End update cart
?>
<html>
<head>
<title>Shopping Cart</title>
</head>
<body bgcolor="#FFFFFF" link="#003399" alink="#FF9933" vlink="#996633" text="#000000">
<?
$url = $PHP_SELF;
if(!$ShoppingCard)
$url .= "?session=$session";
echo "<FORM ACTION=$url method=post>\n";
?>
<a name="top"></a>
<img align=left width=171 height=27 alt="Shopping Cart" src="cart.gif">
<br clear=left>
<table align=center border="0" cellpadding="5" cellspacing="0">
<tr valign="top" bgcolor="#CCCC99">
<td width=500 valign="TOP"><font face="Verdana,Helvetica,Arial" size="+2"><b><CENTER>Shopping
Cart Items</b></font></td>
</center>
</TABLE>
<TABLE BORDER=1 cellpadding=2>
<TR>
<TD>Product</TD><TD>Price</TD><TD>Quantity</TD><TD>Total</TD><TD> </TD>
<?
if($ShoppingCart)
{
$session = $ShoppingCart;
}
$contents = $cart->display_contents($table,$session);
if($contentsїproduct]ї0] != "")
{
$x = 0;
while($x != $cart->num_items($table,$session))
{
echo "<TR><TD>".$contentsїproduct]ї$x]. "</TD><TD>".$contentsїprice]ї$x]. "</TD>\n";
$product = $contentsїproduct]ї$x];
echo "<TD><INPUT TYPE=text size=3 name=quantityї$product] ";
echo "value="".$contentsїquantity]ї$x]. ""></TD>";
echo "<TD>\$".$contentsїtotal]ї$x]. "</TD>\n";
echo "<TD><A HREF="view_cart.php?remove=".urlencode($contentsїproduct]ї$x]);
echo "".(!$ShoppingCart? "&session=$session": ""). "">Remove</A>";
$x ++;
}
echo "</TD><TR></TABLE>";
echo "<BR><B>Total Order Amount: \$".$cart->cart_total($table,$session). "</B>";
echo "<BR><INPUT TYPE=submit name=modify value="Recalculate Order">";
echo "<BR><BR>";
echo "Back to the <A HREF="store.php".(!$ShoppingCart? "?session=$session": ""). "">";
echo "Products</A> page";
}
else
echo "There are no items in your shopping cart.";
?>
</FORM>
</BODY>
</HTML>
and the main file is shoppingcart.php that both of them link through to...
Code: Select all
// this session will only be established the first time that the parent page is loaded.
if(!$session && !$ShoppingCart) //make sure this hasn't already been established
{
$session = md5(uniqid(rand())); //creates a random session value
// sets a cookie with the value of session. On my pages, I do a simple test to see if
// the cookie exists on the user's machine ( if($ShoppingCart) ) if it does exist,
// I don't send the session ID's around with every page they visit, I just use the values
// in the cookie.
//set the cookie to remain for 2 hours
SetCookie( "ShoppingCart", "$session",time()+7200);
}
class Cart
{
function add_item($table,$session,$product,$quantity)
{
// Checks to see if they already have that product in their list
$in_list = "SELECT * FROM $table WHERE session='$session' ";
$in_list .= "AND product='$product'";
$result = mysql_query( "$in_list");
$num_rows = mysql_num_rows($result);
// they don't have that product in their cart? Put it in.
if($num_rows == 0)
{
$sql = "INSERT INTO $table (session,product,quantity) VALUES ";
$sql .= "('$session','$product','$quantity')";
mysql_query( "$sql");
}
// They have the product in their cart already? Add the quantity they specified
// to the product they have in their cart
else
{
$row = mysql_fetch_array($result);
$quantity = $quantity + $rowїquantity];
$sql = "UPDATE $table SET quantity='$quantity' WHERE ";
$sql .= "session='$session' AND product='$product'";
mysql_query( "$sql");
}
}
// delete a specified item
function delete_item($table,$session,$product)
{
mysql_query( "DELETE FROM $table WHERE session='$session' AND product='$product'");
}
// modifies a quantity of an item
function modify_quantity($table, $session, $product, $quantity)
{
$sql = "UPDATE $table SET quantity='$quantity' ";
$sql .= "WHERE session='$session' AND product='$product'";
mysql_query( "$sql");
}
// clear all content in their cart
function clear_cart($table,$session)
{
mysql_query( "DELETE FROM $table WHERE session='$session'");
}
//add up the shopping cart total
function cart_total($table,$session)
{
$result = mysql_query( "SELECT * FROM $table WHERE session='$session'");
if(mysql_num_rows($result) >0)
{
while($row = mysql_fetch_array($result))
{
// look up the item in inventory
$price_from_inventory = "SELECT price FROM inventory WHERE ";
$price_from_inventory .= "product = '$rowїproduct]'";
$result_inventory = mysql_query( "$price_from_inventory");
$row_price = mysql_fetch_array($result_inventory);
//calculate the total
$total = $total + ($row_priceїprice]*$rowїquantity]);
}
}
return $total;
}
function display_contents($table,$session)
{
$count = 0;
$result = mysql_query( "SELECT * FROM $table WHERE session='$session'");
while($row = mysql_fetch_array($result))
{
$result_inv = mysql_query( "SELECT * FROM inventory WHERE product='$rowїproduct]'");
$row_inventory = mysql_fetch_array($result_inv);
$contentsї "product"]ї$count] = $row_inventoryїproduct];
$contentsї "price"]ї$count] = $row_inventoryїprice];
$contentsї "quantity"]ї$count] = $rowїquantity];
$contentsї "total"]ї$count] = ($row_inventoryїprice] * $rowїquantity]);
$count ++;
}
$total = $this->cart_total($table,$session);
$contentsї "final"] = $total;
return $contents;
}
function num_items($table, $session)
{
$result = mysql_query( "SELECT * FROM $table WHERE session='$session'");
$num_rows = mysql_num_rows($result);
return $num_rows;
}
}
?>
An example of what is happening is this:
http://hermes.dcs.bbk.ac.uk/~gassi01/New/store.php?
Thanks
don_assi