Jump to content

Shopping cart/session/mysql question


flit

Recommended Posts

Hi there,

 

I am trying to code a simple shopping cart but I am a complete newbie and I have no idea how to call the information saved in the session [file] and in the mysql database to show the visitors what they have added into the shopping cart.

 

I created a php file called A.php with the following code:

 

<a href="cart.php?action=add&id=1">Add to cart</a> <br>

<a href="cart.php?action=add&id=2">Add to cart</a> <br>

<a href="cart.php?action=add&id=3">Add to cart</a> <br>

---------------------------------------------------------

I created anther php file called cart.php with the following code:

 

<?php

session_start();

?>

 

<?php

$cart = $_SESSION['cart'];

if ($cart) {

$cart .= ','.$_GET['id'];

} else {

$cart = $_GET['id'];

}

$_SESSION['cart'] = $cart;

 

function writeShoppingCart() {

$cart = $_SESSION['cart'];

if (!$cart) {

return '<p>You have no items in your shopping cart</p>';

} else {

// Parse the cart session variable

$items = explode(',',$cart);

$s = (count($items) > 1) ? 's':'';

return '<p>You have '.count($items).' item'.$s.' in your shopping cart</a></p>';

}

}

 

// Connecting, selecting database

$link = mysql_connect('localhost', 'root', 'mypassword)

    or die('Could not connect: ' . mysql_error());

echo 'Connected successfully';

mysql_select_db('testing') or die('Could not select database');

while($row = mysql_fetch_array($result))

{

 

// Don't know how to call the shopping cart info from the session and database

}

?>

 

<?php

echo writeShoppingCart();

?>

--------------------------------------------------

Can anyone please help me with this issue?

Link to comment
https://forums.phpfreaks.com/topic/100192-shopping-cartsessionmysql-question/
Share on other sites

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.