Sending Info to the MySQL DB...
Posted: Wed Feb 19, 2003 3:09 pm
Hi Guys... I'm a little new to all this.. and need someone's help.
I have a problems with the below files
- when I select 'Add Item' on first file (store.php) it does not send anything through to the database... it should be sending info to a table named shopping? Can you trace if there is a discrepancy in the script?
someone suggested to use a $_POST function... but not sure where to use that and why it would make much difference....
here are the files:
store.php
and
view_cart.php (this is the file which is supposed to pick up the item that is added and add to the basket)...
and the include file that holds all the main info:
I appreciate all your help with this...
Thanks
don_assi
I have a problems with the below files
- when I select 'Add Item' on first file (store.php) it does not send anything through to the database... it should be sending info to a table named shopping? Can you trace if there is a discrepancy in the script?
someone suggested to use a $_POST function... but not sure where to use that and why it would make much difference....
here are the files:
store.php
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>view_cart.php (this is the file which is supposed to pick up the item that is added and add to the basket)...
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>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;
}
}
?>Thanks
don_assi