/    Sign up×
Community /Pin to ProfileBookmark

Shopping cart not working properly

Hello Everyone,

I have been trying to get this shopping cart working for a while but I honestly do not know where the problem is. Either I am not creating the seseions properly. However, my primary worry it is not displaying the product in the cart. I ahve tried MANY, MANY forums no one knows what’s happening?

[code=php]<?php
session_start();

// get the product id
$DVDID = isset($_GET[‘DVDID’]) ? $_GET[‘DVDID’] : “”;
$name = isset($_GET[‘NameOfTheDVD’]) ? $_GET[‘NameOfTheDVD’] : “”;
require “connect.php”;
$query = “SELECT `DVDID`, `NameOfTheDVD`, `Quantity` FROM `DVD`”;
$stmt = $dbhandle->prepare($query);
$stmt->execute();

/*
* check if the ‘cart’ session array was created
* if it is NOT, create the ‘cart’ session array
. */
if(!isset($_SESSION[‘cart’])){
$_SESSION[‘cart’][$DVDID];
$_SESSION[‘cart’][$name];
$_SESSION[‘cart’][$Quantity];

}

// check if the item is in the array, if it is, do not add
if(in_array($DVDID, $_SESSION[‘cart’])){
// redirect to product list and tell the user it was added to cart
header(‘Location: shop.php?action=exists&DVDID’ . $DVDID . ‘&name=’ . $name);
}

// else, add the item to the array
else{
array_push($_SESSION[‘cart’][] = $DVDID);
// redirect to product list and tell the user it was added to cart
header(‘Location: shop.php?action=add&DVDID’ . $DVDID . ‘&name=’ . $name);
}

?>[/code]

Above is my AddToCart file.

[code=php]<?php

$action = isset($_GET[‘action’]) ? $_GET[‘action’] : “”;

$name = isset($_GET[‘NameOfTheDVD’]) ? ($_GET[‘NameOfTheDVD’]): “”;
$DVDID = isset($_GET[‘DVDID’]) ? ($_GET[‘DVDID’]) : “”;
$Quantity =isset($_GET[‘Quantity’]) ? ($_GET[‘Quantity’]) : “”;
if($action==’add’){
echo “<h3>” . $DVDID . ” was added to your cart.</h3>”;
}
if($action==’exists’){
echo “<h3>” . $DVDID . ” already exists in your cart.</h3>”;
}
require “connect.php”;

$query = “SELECT `DVDID`, `NameOfTheDVD`, `Quantity` FROM `DVD`”;
$stmt = $dbhandle->prepare($query);
$stmt->execute();

$num = $stmt->rowCount();

if($num>0){
echo “<table border=’3′ cellpadding=’10’ position=’relative` bottom= ‘450px’;>”;//start table

// our table heading
echo “<tr>”;
echo “<th class=’textAlignLeft’>DVD Name</th>”;
echo “<th class=’textAlignLeft’>DVD ID</th>”;
echo “<th>Quantity</th>”;
echo “<th>Action</th>”;
echo “</tr>”;

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){

//creating new table row per record
echo “<tr>”;

echo “<td>” . $row[‘NameOfTheDVD’] . “</td>”;
echo “<td>” . $row[‘DVDID’] . “</td>”;
echo “<td>” . $row[‘Quantity’] . “</td>”;
echo “<td class=’textAlignCenter’>”;
echo “<a href=’AddToCart.php?id={$DVDID}&name={$name}’ class=’customButton’>”;
echo “<img src=’Add.png’ title=’Add To Cart’ />”;
echo “</a>”;
echo “</td>”;
echo “</tr>”;

}

echo “</table>”;
}

// no products in the database
else{
echo “No products found.”;
}

print_r($_GET);

?>[/code]

Above is my shop page where the products are listed.

HELPPPP!!!!

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @mfredy92 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...