Strange behaviour of Mysql_fetch_array + Submit button

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

Hopefully I submit this to the correct list.
 
I'm completely new to PHP and am trying to get some sort of shopping
cart working based on an example I found on the WWW.
I have an include page which contains all cart functions (add to cart
function displayed below), and a main page in which I display a form to
show all products with a submit button to add the selected item +
quantity to a cart db-table.
 
The product display + textbox+ submit button work fine, but when I click
the submit button the product just above the one selected is added to
the cart table in the database together with the quantity given in the
correct textfield (I hope you can visualise what I mean).
 
I have added the ADD TO CART code and the main page code below and hope
anyone can help me solve this mystery (which it is to me anyway, but
hopefully not for you guys).
 
Thanx,
 
Bob
 
************************************************************************
**************************
 
/* function code */
 
        function add_item($table, $session, $product, $quantity) { 
            $qty = $this->check_item($table, $session, $product); 
            if($qty == 0) { 
                $query = "INSERT INTO $table (session, product,
quantity) VALUES "; 
                $query .= "('$session', '$product', '$quantity') "; 
                mysql_query($query); 
            } else { 
                $quantity += $qty; 
                $query = "UPDATE $table SET quantity='$quantity' WHERE
session='$session' AND "; 
                $query .= "product='$product' "; 
                mysql_query($query); 
            } 
        } 

/* End function code */
 
************************************************************************
**************************
 
/* The php code in the page */
 
<?php 
    include("shoppingcart.php");
    include("db_connect.php");  
 

    $cart = new Cart; 
 $Query_result = @mysql_query ( "SELECT product, quantity, category,
description, price FROM inventory ORDER BY price " );
   if (!$Query_result)
   {
    echo ( "<p>Error performing query.".mysql_error()."</P>");
    exit();
   };
   $counter = 1;
   while ($row = mysql_fetch_array($Query_result) )
   {
    $selected_product = $row["product"];
    $selected_quantity_available = $row["quantity"];
    $selected_category = $row["category"];
    $selected_description = $row["description"];
    $selected_price = $row["price"];
    echo "<form name=\"form$counter\" method=\"get\">";
    echo ("Product : ".$selected_product."<br> Quantity :
".$selected_quantity_available."<br> Description : ".
$selected_description."<br> Category : ".$selected_category."<br> Price
: ".$selected_price." EUR <br>");
    echo "<input type=\"text\" name=\"selected_quantity\">";
    echo "<input type=\"submit\" name=\"Submitname\"
value=\"Submit$counter\">";
    echo "<br>";
    echo "Session variable value = $session ...";
    echo "<br>";
    echo ("Selected product: ".$row["product"]);
    echo "<br>";
    echo "<hr>";
    echo "</form>";
    $counter++;
    if ($Submitname == "Submit$counter")
    {
     echo ($row["product"]);
     echo "<br>";
     echo ("Quantity = ".$selected_quantity);
     echo "<br>";
     $cart->add_item("shopping", $session, $row["product"],
$selected_quantity);
    };
   };
?> 

/* End page php code */
 
************************************************************************
**************************





Bob GERVAIS
MIS Manager 
XRT, Think Financial Value Chain 
Tel : +32-3-206 92 11 
Fax : +32-3-206 92 00
Tel direct: +32-3-727 29 46 
<mailto:bgervais@xxxxxxxxxx <mailto:bgervais@xxxxxxxxxx> > 
<http://www.xrt.com <http://www.xrt.com/> >

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux