I have been trying to build a shopping cart with PHP and MySQL as a learning experiment. I am stuck at passing the ordered items from catalogue to the shopping cart.
After many experiments I figured the best way to do this is through Session variables, but I cannot make it work.
Each row of the record is like this:
Item_name, Qty, Price
This is what I am doing (which is not working): The catalogue page sends the form variables to a page called CART1 which is:
session_start();
$PCCode = $_POST["PCCode"];// name of product
$PQty = $_POST["PQty"];// order quantity for that product
$PPrice = $_POST["PPrice"];
$NPP = $_POST["NPP"];//number of products in product database
$_SESSION["NPP"]= $NPP;
for ($i=1;$i<=$NPP;$i++)
{
if (isset($PCCode[$i])) $_SESSION["PCCode"][$i] = $PCCode[$i];
if (isset($PQty[$i])) $_SESSION["PQty"][$i] = $PQty[$i];
if (isset($PPrice[$i])) $_SESSION["PPrice"][$i] = $PPrice[$i];
$_SESSION["Ordered"] = 1;// order has been made
}
header("Location: Catalogue.php");
So this page is supposed to register the variables in a 2 dimensional session array and redirect back to the catalogue page.
THE PROBLEM IS:
- I can get one row of data registered but the next item ordered will replace this one.
Any idea what am I doing wrong? What is the right way to do it?
PCCode, but the comment says it's a name, not a code. I'm not sure whatPCCodestands for? Your other variables are a bit strange as well:PQtyinstead ofQuantity, and so on. What is this urge to abbreviate everything into oblivion? Variable names are there to clarify what a variable contains, not to obfuscate it. Anyway, we don't know what your form actually submits, so we cannot tell how to process it, but it would be nice if the form data contained an unique identifier for the product.for ($i=1...you always start from 1, disregarding how many other items are already in the session. So you always overwrite the Session items from the beginning, every timevar_dump($_POST)to make sure the contents meet your expectations.