Nothing being sent to DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
don_assi
Forum Newbie
Posts: 16
Joined: Tue Dec 03, 2002 7:40 am

Nothing being sent to DB

Post 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)) 
&#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>
Would really appreciate your help.

Thanks
don_assi
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post 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.
don_assi
Forum Newbie
Posts: 16
Joined: Tue Dec 03, 2002 7:40 am

Post 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) 
   &#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> </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 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 
&#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; 
?>
An example of what is happening is this:

http://hermes.dcs.bbk.ac.uk/~gassi01/New/store.php?

Thanks
don_assi
Post Reply