Jump to content

Want to build a simple shoppng cart.


gex80

Recommended Posts

Ok I basically want to build a simple php shopping cart. I already have a database with products and product IDs, and each product page has their product ID associated with it by asking the database for it's ID.

 

Now how would I go about building a shopping cart? I already researched sessions and cookies and came to the conclusion that a session would be best since not all browsers use cookies or have them turned on.

 

I how ever don't understand how to use sessions for a shopping cart.

 

I know the first line the code in my php document has to have session_start();

 

How do I go about adding values to the session?

Link to comment
Share on other sites

Here is a simple login form that will help create the session. 

You'll need a login table with username and password fields (and don't forget the id field, auto-increment).

 

<?php
session_start();
require("db.php");

if(isset($_SESSION['SESS_LOGGEDIN']) == TRUE) 
	{
		header("Location: " . $config_basedir);
	}
if($_POST['submit'])
{
	$loginsql = "SELECT * From logins
	WHERE username = '" . $_POST['userBox']
	. "' AND password = '" . $_POST['passBox']
	. "'";
	$loginres = mysql_query($loginsql);
	$numrows = mysql_num_rows($loginres);

	if($numrows == 1)
	{
		$loginrow = mysql_fetch_assoc($loginres);
		session_register("SESS_LOGGEDIN");
		session_register("SESS_USERNAME");
		session_register("SESS_USERID");

		$_SESSION['SESS_LOGGEDIN'] = 1;
		$_SESSION['SESS_USERNAME'] = $loginrow['username'];
		$_SESSION['SESS_USERID'] = $loginrow['id'];

		$ordersql = "SELECT id FROM orders
		WHERE customer_id = " . $_SESSION['SESS_USERID']
		. " AND status < 2";
		$orderres = mysql_query($ordersql);
		$orderrow = mysql_fetch_assoc($orderres);

		session_register("SESS_ORDERNUM");
		$_SESSION['SESS_ORDERNUM'] = $orderrow['id'];

		header("Location: " . $config_basedir);
	}
	else
	{
		header("Location: http://" . $HTTP_HOST	. $SCRIPT_NAME . "?error=1");
	}
}

else
{
	require("header.php");

?>

<div id="main">
        <h1>Customer Login</h1>
        
	<?php
   		if($_GET['error'])
		{
			echo "Incorrect username/password";
		}
	?>
        	          

<form action="<?php echo $SCRIPT_NAME; ?>" method="POST">
<table>
<tr>
    	<td>Username</td>
        <td><input type="textbox" name="userBox">
    </tr>
    
    <tr>
    	<td>Password</td>
        <td><input type="password" name="passBox">
    </tr>
    
    <tr>
    	<td></td>
        <td><input type="submit" name="submit" value="Log in">
     </tr>
</table>
</form> 
      
</div><!--end main-->     
      
<?php
}

require("footer.php");
?>

 

This checks to see if the user is logged in, if they're not, they're shown the login form.

Upon logging in, their session is set.

Link to comment
Share on other sites

I don't think there is such a thing as a simple shopping cart.  And I'd be surprised if it can be done without many many  posted replies but I think this website has excellent tutorials and

"Effortless e-commerce" by Larry Ullman is probably the best way to go if you want to DIY.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.