Jump to content

[SOLVED] help with php shopping cart, suspected problem in session


swissbeets

Recommended Posts

i have gone through this tutorial

http://whoyouknow.co.uk/php/shop/

and put everything in its expected places

 

but

1: the product id is not being shown, it will constantly only bring up the first product of my database. (name and price)

2: when i go back to the products.php and click another product, nothing is updated.

 

i have little experience with sessions but have been reading a lot about them still with no prevail

 

this is the products.php

 

<?php if (!isset($_SESSION['cart']))

{ session_start();

};

 

require_once("includes/connection.php");

require_once("includes/functions.php");

 

while($row = mysql_fetch_array($product_set)){

 

 

 

    echo "<p>";

    echo $row['product_name']."<br />";

    echo "$".$row['product_price']."<br />"."<br />";

    show_picture($row); 

 

echo "<br/>";

 

 

?>

    <form action="shoppingcart.php?prod=<?php echo urlencode($row['product_id']);?>&action=add" method="post">

    <input type="submit" name="submit" value="Add to Shopping Cart" /><?php

echo "</p>";

}  ?>// the values are being transferred in the URL to my shoppingcart.php

 

this is my shopping cart

 

<?php require_once("includes/connection.php"); ?>

<?php require_once("includes/functions.php"); ?>

<?php include("includes/header.php"); ?>

 

<?php

 

    $product_id = $_GET[prod];    //the product id from the URL

    $action    = $_GET[action]; //the action from the URL

 

    //if there is an product_id and that product_id doesn't exist display an error message

    if($product_id && !productExists($product_id)) {

        die("Error. Product Doesn't Exist");

    }

 

    switch($action) {    //decide what to do   

   

        case "add":

            $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id

echo "success";

        break;

       

        case "remove":

            $_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id

            if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items.

        break;

       

        case "empty":

            unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart.

        break;

   

    }

 

   

?>

 

 

 

 

<?php  

 

    if($_SESSION['cart']) {    //if the cart isn't empty

        //show the cart

       

        echo "<table border=\"1\" padding=\"3\" width=\"40%\">";    //format the cart using a HTML table

       

            //iterate through the cart, the $product_id is the key and $quantity is the value

            foreach($_SESSION['cart'] as $product_id => $quantity) {   

               

                //get the name, description and price from the database - this will depend on your database implementation.

                //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection

                $sql = sprintf("SELECT product_name, product_price FROM products WHERE product_id = $product_id;",

                                $product_id);

                   

                $result = mysql_query($sql);

                   

                //Only display the row if there is a product (though there should always be as we have already checked)

                if(mysql_num_rows($result) > 0) {

               

                    list($name, $price) = mysql_fetch_row($result);

               

                    $line_cost = $price * $quantity;        //work out the line cost

                    $total = $total + $line_cost;            //add to the total cost

               

                    echo "<tr>";

                        //show this information in table cells

                        echo "<td align=\"center\">$name</td>";

                        //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product

                        echo "<td align=\"center\">$quantity <a href=\"$_SERVER[php_SELF]?action=remove&prod=$product_id\">Remove</a></td>";

                        echo "<td align=\"center\">$line_cost</td>";

                   

                    echo "</tr>";

                   

                }

           

            }

           

            //show the total

            echo "<tr>";

                echo "<td colspan=\"2\" align=\"right\">Total</td>";

                echo "<td align=\"right\">$total</td>";

            echo "</tr>";

           

            //show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation

            echo "<tr>";

                echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[php_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>";

            echo "</tr>";       

        echo "</table>";

       

       

   

    }else{

        //otherwise tell the user they have no items in their cart

        echo "You have no items in your shopping cart.";

       

    }

?>

 

 

alot of information but any help would be greatly appreciated since i have been trying to find the problem for hours

Any form that uses a session must include session_start; at the very beginning, before any html, or any php, except for the line <?php which obviously starts your php code. What i like to do is make a file called auth.php and do a session_start; in the beginning there, and then do any checks you mgiht have. And require_once this file at the very beginning of the code.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.