Simplified Shopping cart - why won't this work?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Simplified Shopping cart - why won't this work?

Post by simonmlewis »

Code: Select all

session_start(); 
$idinc = ($idinc + 1);
$buyid=('buyid'.$idinc);
if(isset($_REQUEST['buyid']))
{
    $buyid = $_REQUEST['buyid'];
    $_SESSION['buyid']=$buyid;
} else { $buyid=$_SESSION['buyid'];
}
echo "$buyid";

<a href='index.php?page=product&product=$row->id&s=$row->subid&c=$row->catid&cname=$row->catname&sname=$row->subname&menu=sub&buyid=$row->id'>Buy Now</a>
I am trying to post the ID of a product to an auto-incremental 'buyid' in a global variable in the session.

This isn't storing anything. Maybe I am going about this the wrong way. The idea being that you can store all the product ids you want in your "cart", and then you extract it from the database later on, based on the "buyid" in the Cart page.

Can anyone help me please?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Alex-V
Forum Newbie
Posts: 17
Joined: Mon Jul 19, 2010 3:53 pm

Re: Simplified Shopping cart - why won't this work?

Post by Alex-V »

You need to store last buyid in your db. Session are separate for each user you can't increase a value that is not set.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Simplified Shopping cart - why won't this work?

Post by simonmlewis »

I don't understand why you can't store a session variable in this way.

But would you then say that I ought to have a 'cartdb' and a 'orderdb' to store the shopping cart, and then the ORDERS separately?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Alex-V
Forum Newbie
Posts: 17
Joined: Mon Jul 19, 2010 3:53 pm

Re: Simplified Shopping cart - why won't this work?

Post by Alex-V »

You don't need 2 db's to store that data. Just two tables. You can assign one id to one user via session but then next user will get the same id because your script doesn't know what is the last it (the one that last user got). I hope you understand me :D
Post Reply