3

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?

10
  • 1
    Do each of your products have an unique identifier? I though it could be your PCCode, but the comment says it's a name, not a code. I'm not sure what PCCode stands for? Your other variables are a bit strange as well: PQty instead of Quantity, 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. Commented Oct 29 at 21:28
  • 1
    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 time Commented Oct 29 at 21:48
  • 1
    @KIKOSoftware That's overly picky. "qty" is a very common abbreviation for quantity -- programmers and laymen have been using it for decades (I remember it from my COBOL days in the 70's). Commented Oct 29 at 21:48
  • 1
    Use var_dump($_POST) to make sure the contents meet your expectations. Commented Oct 29 at 21:49
  • 2
    Yes, I unilaterally closed this question as it needs clarity and debugging details. Multiple users would like to see more information provided about the data being submitted. The closure is a request for improvement; it is not a sign that you should give up. What does the form look like? Might you transpose the form field naming convention so that it mirror's your SESSION data structure? Wouldn't that simplify things? Commented Nov 3 at 1:15

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.