Hello everyone,

I moved forward to purchase a book with PHP examples. This ecommmerce example did not make much sense to me at a certain point, it became too technial and I was unable to follow along with explicit understanding. There are two pieces of code that are problematic starting with the shopping cart:

<html>
    <head>
        <title>Here is Your Shopping Cart!</title>
        </head>
    <body>
        <dev align="center">
            You currently have
            
            <?php
            $sessid = session_id();
            
            //display number of products in cart
            $query = "SELECT * FROM carttemp WHERE carttemp_sess = '$sessid'";
            $results = mysql_query($query)
                or die (mysql_query());
            $rows = mysql_num_rows($results);
            echo $rows;
            ?>
            
            product(s) in your cart.<br>
            
            <table border="1" align="center" cellpadding="5">
                <tr>
                    <td>Quantity</td>
                    <td>Item Image</td>
                    <td>Item Name</td>
                    <td>Price Each</td>
                    <td>Extended Price</td>
                    <td></td>
                    <td></td>
                <?php
                $total = 0;
                while ($row = mysql_fetch_array($results)) {
                    echo"<tr>";
                extract($row);
                $prod = "SELECT * FROM products " .
                        "WHERE products_prodnum='$carttemp_prodnum'";
                $prod2 = mysql_query($prod);
                $prod3 = mysql_fetch_array($prod2);
                extract ($prod3);
                echo "<td>
                        <form method=\"POST\" action=\"modcart.php?action=change\">
                        <input type=\"hidden\" name=\"modified_hidden\"
                            value=\"$carttemp_hidden\">
                        <input type=\"text\" name\"modified_quan\" size=\"2\"
                            value=\"$carttemp_quan\">";
                echo "</td>";
                echo "<td>";
                echo "<a href=\"getprod.php?prodid=" . $products_prodnum . "\">";
                echo "THUMBNAIL<br>IMAGE</a></td>";
                echo "<td>";
                echo "<a href=\"getprod.php?prodid=" . $products_prodnum . "\">";
                echo $products_name;
                echo "</a></td>";
                echo "<td align=\"right\">";
                echo $products_price;
                echo "</td>";
                echo "<td align=\"right\">";
                //get extended price
                $extprice = number_format($products_price * $carttemp_quan, 2);
                echo $extprice;
                echo "</td>";
                echo "<td>";
                echo "<input type=\"submit\" name=\"Submit\"
                        value=\"Change Qty\">
                    </form></td>";
                echo "<td>";
                echo "<form method=\"POST\" action=\"modcart.php?action=delete\">
                <input type=\"submit\" align=\"center\" name=\"modified_hidden\"
                    value=\"Delete Item\">
                </form></td>";
                echo "</tr>";
                //add extended price to total
                $total = $extprice + $total;
                
                }
                ?>
                    <tr>
                        <td colspan=\"4\" align=\"right\">
                            Your total before shipping is:</td>
                        <td align=\"right\"> <?php echo number_format($total, 2); ?></td>
                        <td></td>
                        <td>
                    <?php
                    echo "<form method=\"POST\" action=\"modcart.php?action=empty\">
                            <input type=\"hidden\" name=\"carttemp_hidden\"
                                value=\" ";
                    if (isset($carttemp_hidden)) {
                        echo $carttemp_hidden;
                    }
                    
                    echo "\">";
                    echo "<input type=\"submit\" name=\"Submit\" value=\"Empty Cart\">
                        </form>";
                    ?>
                    
                    </tr>
                        </table>
            <form method="POST" action="checkout.php">
                <input type="submit" name="Submit" value="Proceed to Checkout">
                    </form>
            <a href="4sightshop.php">Go back to the main page</a>
            </div>
        </body>
        </html>

And the following code is used to modify the shopping cart:

<?php
session_start();
    mysql_select_db("ecommerce");
    if (isset($_POST['qty'])){
        $qty = $_POST['qty'];
    }
    if (isset($_POST['products_prodnum'])) {
        $prodnum = $_POST['products_prodnum'];
    }
    if (isset($_POST['modified_quan'])) {
        $modified_quan = $_POST['modified_quan'];
    }
    $sess = session_id();
    $action = $_REQUEST['action'];
    
    switch ($action) {
        case "add":
            $query = "INSERT INTO carttemp (
            carttemp_sess,
            carttemp_quan,
            carttemp_prodnum)
            VALUES ('$sess', '$qty', '$prodnum')";
        $message = "<div align=\"center\"><strong>Item added</strong></div>";
        break;
    
    case "change":
        $query = "UPDATE carttemp
                  SET carttemp_quan = '$modified_quan'
                  WHERE carttemp_hidden = '$modified_hidden'";
        $message = "<div align='center'><strong>Quantity changed.</strong></div>";
    break;

case "delete":
        $query = "DELETE FROM carttemp
                  WHERE carttemp_hidden = '$modified_hidden'";
        $message = "<div align='center'><strong>Item deleted.</strong</div>";
        break;
    
case "empty":
        $query = "DELETE FROM carttemp WHERE carttemp_sess = '$sess'";
        $message = "<div align='center'><strong>Cart emptied.</strong></div>";
        break;
        
    }
    
    $results = mysql_query($query)
        or die(mysql_error());
        
    echo $message;
    
    include("cart.php");
?>

The code to change quantity and delete individual items do not work; however the empty cart and add items to cart functions are fine. The notice I receive when running the code is:

NOTICE: Undefined variable: modified_quan in C:\...\modcart.php on line 31.
NOTICE: Undefined variable: modified_hidden in C:\...\modcart.php on line 31.

Any suggestions or observations offered are greatly appreciated!!

you you please use the [/code ] it will number it and such but for your problem you didnt define $modified_hidden

$modified_quan[code=php] [/code ]
it will number it and such but for your problem you didnt define $modified_hidden

$modified_quan

you you please use CODE tags, they will number it and such but for your problem you didnt define $modified_hidden

$modified_quan

Thank you, I was able to find the typos related to this and correct them... but now I am having a problem with the Total price display. The price listed is erratic and will not persist, especially when the quantity of items is changed from the cart.

I have 3 items, one 395.00, one 695.00, and one 1,250.00. The code can handle two 395.00 items calculating the total at 790.00. The code can handle one 695.00 item for a grand total of 1,485.00. The 1,250.00 item is calculated as a 1.00, two 695.00 items are calculated as a 1.00, and three 395.00 items are calculated as a 1.00. Logically, the code makes sense to me but something funky is happening here. Note that when the cart is modified, the $total is reset to a value of 0.

$total = 0;     
while ($row = mysql_fetch_array($results)) {
                    echo"<tr>";
                extract($row);
                $prod = "SELECT * FROM products " .
                        "WHERE products_prodnum='$carttemp_prodnum'";
                $prod2 = mysql_query($prod);
                $prod3 = mysql_fetch_array($prod2);
                extract ($prod3);
                echo "<td>
                       form method=\"POST\" action=\"modcart.php?action=change\">
                        <input type=\"hidden\" name=\"modified_hidden\"
                            value=\"$carttemp_hidden\">
                        <input type=\"text\" name=\"modified_quan\" size=\"2\"
                            value=\"$carttemp_quan\">";
                echo "</td>";               echo "<td>";
                echo "<a href=\"getprod.php?prodid=" . $products_prodnum . "\">";
                echo "THUMBNAIL<br>IMAGE</a></td>";
                echo "<td>";
                echo "<a href=\"getprod.php?prodid=" . $products_prodnum . "\">";
                echo $products_name;
                echo "</a></td>";
                echo "<td align=\"right\">";
                echo $products_price;
                echo "</td>";
                echo "<td align=\"right\">";
                //get extended price
$extprice = number_format($products_price * $carttemp_quan, 2);
                echo $extprice;
                echo "</td>";
                echo "<td>";
                echo "<input type=\"submit\" name=\"Submit\"
                        value=\"Change Qty\">
                    </form></td>";
                echo "<td>";
                echo "<form method=\"POST\" action=\"modcart.php?action=delete\">
                <input type=\"hidden\" align=\"center\" name=\"modified_hidden\"
                    value=\"$carttemp_hidden\">";
                echo "<input type=\"submit\" name=\"Submit\"
                    value=\"Delete Item\">
                </form></td>";
                echo "</tr>";
//add extended price to total
                $total = $extprice + $total;     
                }
?>
                    <tr>
                        <td colspan=\"4\" align=\"right\">
                            Your total before shipping is:</td>
                        <td align=\"right\"> <?php echo number_format($total, 2); ?></td>
                        <td></td>
                        <td>

Here is the modify code:

 case "change":
        $query = "UPDATE carttemp
                  SET carttemp_quan = '$modified_quan'
                  WHERE carttemp_hidden = '$modified_hidden'";
        $message = "<div align='center'><strong>Quantity changed.</strong></div>";
    break;

    include("cart.php");

Edited 3 Years Ago by happygeek: fixed formatting

all i could see is this your $total = 0 the whole way from the top
so when you add extprice + $total it's what ever $extprice = + 0

and change the second statement to a plus + sign not a *

<?PHP  
$total = $extprice + $total; 


$extprice = number_format($products_price * $carttemp_quan, 2);
?>

i give that a whirl

all i could see is this your $total = 0 the whole way from the top
so when you add extprice + $total it's what ever $extprice = + 0

and change the second statement to a plus + sign not a *

<?PHP  
$total = $extprice + $total; 


$extprice = number_format($products_price * $carttemp_quan, 2);
?>

i give that a whirl

I'm not sure I understand but okay. I changed the sign as directed and the results are that the quantity of items selected is appended to the total price, two items that cost 395 each now cost 790 + 2 = 792.

Let me know if there is anything else I can provide to help.

This article has been dead for over six months. Start a new discussion instead.