Page 1 of 1

Sending Info to the MySQL DB...

Posted: Wed Feb 19, 2003 3:09 pm
by don_assi
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

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))
&#123;
   echo  "Product: $row&#1111;product]<BR>";
   echo  "Description: $row&#1111;description]<BR>";
   echo  "Price: \$$row&#1111;price]<BR>";
   echo  "<A HREF="view_cart.php?add=".urlencode($row&#1111;product]);
   if (!$ShoppingCart)
      echo  "&session=$session">";
   else
      echo  "">";
   echo  "Add this item to your cart</A><BR><BR>";
&#125;
echo  "For documentation and source, go ";
echo  "<A
HREF="http://www.montana.com/ethan/mycart/example/shop.html">here</A>\n";
?>

</FORM>
</BODY>
</HTML>
and

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)
   &#123;
     if($ShoppingCart)
     $session = $ShoppingCart;

     $result = mysql_query(  "SELECT * FROM inventory WHERE product='$add'");
     $row = mysql_fetch_array($result);

     $cart->add_item($table,$session,$row&#1111;product],1);
   &#125;

   if($remove)
   &#123;
     if($ShoppingCart)
     &#123;
       $session = $ShoppingCart;
     &#125;

     $result = mysql_query(  "SELECT * FROM inventory WHERE product='$remove'");
     $row = mysql_fetch_array($result);
     $cart->delete_item($table,$session,$row&#1111;product]);
   &#125;
   if($modify)
   &#123;
     $contents = $cart->display_contents($table,$session);
     for($i = 0; $i < sizeof($quantity); $i++)
     &#123;
       $oldquan = $contents&#1111;quantity]&#1111;$i];
       $product = $contents&#1111;product]&#1111;$i];
       $newquan = $quantity&#1111;$product];
       $cart->modify_quantity($table,$session,$product,$newquan);
     &#125;
   &#125;
         // 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>&nbsp;</TD>

<?

  if($ShoppingCart)
  &#123;
    $session = $ShoppingCart;
  &#125;

  $contents = $cart->display_contents($table,$session);
  if($contents&#1111;product]&#1111;0] !=  "")
  &#123;
    $x = 0;
    while($x != $cart->num_items($table,$session))
    &#123;
       echo  "<TR><TD>".$contents&#1111;product]&#1111;$x]. "</TD><TD>".$contents&#1111;price]&#1111;$x]. "</TD>\n";
       $product  = $contents&#1111;product]&#1111;$x];
       echo  "<TD><INPUT TYPE=text size=3 name=quantity&#1111;$product] ";
       echo  "value="".$contents&#1111;quantity]&#1111;$x]. ""></TD>";
       echo  "<TD>\$".$contents&#1111;total]&#1111;$x]. "</TD>\n";
       echo  "<TD><A HREF="view_cart.php?remove=".urlencode($contents&#1111;product]&#1111;$x]);
       echo  "".(!$ShoppingCart?  "&session=$session": ""). "">Remove</A>";
       $x ++;
    &#125;
    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";
  &#125;
  else
    echo  "There are no items in your shopping cart.";
?>
</FORM>
</BODY>
</HTML>
and the include file that holds all the main info:

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
&#123;
   $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);
&#125;
     class Cart
     &#123;
        function add_item($table,$session,$product,$quantity)
        &#123;
            // 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)
           &#123;
              $sql =  "INSERT INTO $table (session,product,quantity) VALUES ";
              $sql .=  "('$session','$product','$quantity')";
              mysql_query( "$sql");
           &#125;
            // They have the product in their cart already?  Add the quantity they specified
            // to the product they have in their cart
           else
           &#123;
              $row = mysql_fetch_array($result);
              $quantity = $quantity + $row&#1111;quantity];
              $sql =  "UPDATE $table SET quantity='$quantity' WHERE ";
              $sql .=  "session='$session' AND product='$product'";
              mysql_query( "$sql");
           &#125;
        &#125;
         // delete a specified item
        function delete_item($table,$session,$product)
        &#123;
           mysql_query( "DELETE FROM $table WHERE session='$session' AND product='$product'");
        &#125;
         // modifies a quantity of an item
        function modify_quantity($table, $session, $product, $quantity)
        &#123;
           $sql =  "UPDATE $table SET quantity='$quantity' ";
           $sql .=  "WHERE session='$session' AND product='$product'";
           mysql_query( "$sql");
        &#125;
         // clear all content in their cart
        function clear_cart($table,$session)
        &#123;
           mysql_query( "DELETE FROM $table WHERE session='$session'");
        &#125;
         //add up the shopping cart total
        function cart_total($table,$session)
        &#123;
           $result = mysql_query( "SELECT * FROM $table WHERE session='$session'");
           if(mysql_num_rows($result) >0)
           &#123;
              while($row = mysql_fetch_array($result))
              &#123;
                  // look up the item in inventory
                 $price_from_inventory =  "SELECT price FROM inventory WHERE ";
                 $price_from_inventory .=  "product = '$row&#1111;product]'";
                 $result_inventory = mysql_query( "$price_from_inventory");
                 $row_price = mysql_fetch_array($result_inventory);
                  //calculate the total
                 $total = $total + ($row_price&#1111;price]*$row&#1111;quantity]);
              &#125;
           &#125;
           return $total;
           &#125;
         function display_contents($table,$session)
         &#123;
         $count = 0;
         $result = mysql_query( "SELECT * FROM $table WHERE session='$session'");
         while($row = mysql_fetch_array($result))
         &#123;
            $result_inv = mysql_query( "SELECT * FROM inventory WHERE product='$row&#1111;product]'");
            $row_inventory = mysql_fetch_array($result_inv);
            $contents&#1111; "product"]&#1111;$count] = $row_inventory&#1111;product];
            $contents&#1111; "price"]&#1111;$count] = $row_inventory&#1111;price];
            $contents&#1111; "quantity"]&#1111;$count] = $row&#1111;quantity];
            $contents&#1111; "total"]&#1111;$count] = ($row_inventory&#1111;price] * $row&#1111;quantity]);
            $count ++;
         &#125;
          $total = $this->cart_total($table,$session);
          $contents&#1111; "final"] = $total;
          return $contents;
         &#125;
         function num_items($table, $session)
         &#123;
            $result = mysql_query( "SELECT * FROM $table WHERE session='$session'");
            $num_rows = mysql_num_rows($result);
            return $num_rows;
         &#125;
    &#125;
?>
I appreciate all your help with this...

Thanks
don_assi