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
https://forums.phpfreaks.com/topic/233161-want-to-build-a-simple-shoppng-cart/
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.

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.

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.