/    Sign up×
Community /Pin to ProfileBookmark

sessions error

Hi im trying to make shoping cart in php..i have problem as i have declared session call session[‘cart’]…but it doesnt like it

my code

[code=php]<?php
$_SESSION[‘cart’];
$product_id = $_GET[id]; //the product id from the URL
$action = $_GET[action]; //the action from the URL

//if there is an product_id and that product_id doesn’t exist display an error message
if($product_id && !productExists($product_id)) {
die(“Error. Product Doesn’t Exist”);
}

switch($action) { //decide what to do

case “add”:
$_SESSION[‘cart’][$product_id]++; //add one to the quantity of the product with id $product_id
break;

case “remove”:
$_SESSION[‘cart’][$product_id]–; //remove one from the quantity of the product with id $product_id
if($_SESSION[‘cart’][$product_id] == 0) unset($_SESSION[‘cart’][$product_id]); //if the quantity is zero, remove it completely (using the ‘unset’ function) – otherwise is will show zero, then -1, -2 etc when the user keeps removing items.
break;

case “empty”:
unset($_SESSION[‘cart’]); //unset the whole cart, i.e. empty the cart.
break;

}

?>[/code]

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@ZABIMar 05.2012 — first you need to start a session
Copy linkTweet thisAlerts:
@hyperionXSMar 05.2012 — first you need to start a session[/QUOTE]

A.K.A. begin your code with session_start()

(Just to be sure he knows what he needs to do)

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

$_SESSION['cart'];
$product_id = $_GET[id]; //the product id from the URL
$action = $_GET[action]; //the action from the URL

//if there is an product_id and that product_id doesn't exist display an error message
if($product_id && !productExists($product_id)) {
die("Error. Product Doesn't Exist");
}
...
[/code]
Copy linkTweet thisAlerts:
@jag420authorMar 06.2012 — Thank you for reply...im getting this new error now

[code=php]Warning: Cannot use a scalar value as an array in C:xampphtdocsshoppingcart.php on line 39[/code]

im passing ID and action from product page...and my cart.php looks something like this..

[code=php]
<?php
session_start();
require_once("dbcon.php");
$_SESSION['cart'];

$product_id = $_GET['id']; //the product id from the URL
$action = $_GET['action']; //the action from the URL

//if there is an product_id and that product_id doesn't exist display an error message
if($product_id && !productExists($product_id)) {
die("Error. Product Doesn't Exist");
}

switch($action) { //decide what to do

case "add":
$_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id
break;

case "remove":
$_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id
if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items.
break;

case "empty":
unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart.
break;

}

?>


<?php
print_r($_SESSION['cart']);

if($_SESSION['cart']) { //if the cart isn't empty
//show the cart

echo "<table border="1" padding="3" width="40&#37;">"; //format the cart using a HTML table

//iterate through the cart, the $product_id is the key and $quantity is the value
foreach($_SESSION['cart'] as $product_id => $quantity) {

//get the name, description and price from the database - this will depend on your database implementation.
//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
$sql = sprintf("SELECT Name, Description, Price FROM products WHERE Pro_ID = %d;",$product_id);

$result = mysql_query($sql);

//Only display the row if there is a product (though there should always be as we have already checked)
if(mysql_num_rows($result) > 0) {

list($name, $description, $price) = mysql_fetch_row($result);

$line_cost = $price * $quantity; //work out the line cost
$total = $total + $line_cost; //add to the total cost

echo "<tr>";
//show this information in table cells
echo "<td align="center">$name</td>";
//along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product
echo "<td align="center">$quantity <a href="$_SERVER[PHP_SELF]?action=remove&id=$product_id">X</a></td>";
echo "<td align="center">$line_cost</td>";

echo "</tr>";

}

}

//show the total
echo "<tr>";
echo "<td colspan="2" align="right">Total</td>";
echo "<td align="right">$total</td>";
echo "</tr>";

//show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation
echo "<tr>";
echo "<td colspan="3" align="right"><a href="$_SERVER[PHP_SELF]?action=empty" onclick="return confirm('Are you sure?');">Empty Cart</a></td>";
echo "</tr>";
echo "</table>";



}else{
//otherwise tell the user they have no items in their cart
echo "You have no items in your shopping cart.";

}

//function to check if a product exists
function productExists($product_id) {
//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
$sql = sprintf("SELECT * FROM products WHERE Pro_ID = %d;",$product_id);

return mysql_num_rows(mysql_query($sql)) > 0;
}
?>[/code]
Copy linkTweet thisAlerts:
@hyperionXSMar 06.2012 — Whould you please be kind to show us the content of shoppingcart.php? Because there is the error.
Copy linkTweet thisAlerts:
@jag420authorMar 06.2012 — above php is the shoppingcart.php...i have only to pages one product

[code=php]
<?php


$query=mysql_query("SELECT * FROM products WHERE Cat_ID='1'") or die(mysql_error());

echo "<br/><table border='1px'>";
echo "<tr>";
echo "<td> <strong>Name</stong> </td>";
echo "<td> <strong>Description</stong> </td>";
echo "<td> <strong>Price</stong> </td>";
echo "<td> <strong>Add to basket</stong> </td>";
echo "<tr>";

while($result=mysql_fetch_array($query))
{


echo "<tr>";
echo "<td>".$result['Name']."</td>";
echo "<td>".$result['Description']."</td>";
echo "<td>".$result['Price']."</td>";
echo "<td><a href='mobiles.php?id=".$result['Pro_ID']."'> Add to basket</a></td>";
echo "<td><a href="cart.php?action=add&id=".$result['Pro_ID']."">Add To Cart</a></td>";
echo "</tr>";

}
echo "</table>";

?>[/code]


so from this page to cart page which is above in previous comment.. thanks
Copy linkTweet thisAlerts:
@hyperionXSMar 06.2012 — Sorry, my bad. I missed the scrolbar in your code.

Please have a print_r($_SESSION) right after session_start() and let me see the output.

Also the line where the error happens is very important for debuging. So please write again here the latest version you have and the exact error you receive.
Copy linkTweet thisAlerts:
@jag420authorMar 06.2012 — [code=php]Notice: Undefined index: cart in C:xampphtdocsiBuycart.php on line 39

Notice: Undefined index: 1 in C:xampphtdocsiBuycart.php on line 39[/code]


when i click add to cart which adds to the cart list display but shows me error...and then if refresh same page it dosnt show any more and double the quantity


after print_r statement

[code=php]
Array ( )
Notice: Undefined index: cart in C:xampphtdocsiBuycart.php on line 38

Notice: Undefined index: 1 in C:xampphtdocsiBuycart.php on line 38

BlackBerry Curve 1 X 130

Total 130

Empty Cart
Continue Shopping
[/code]
Copy linkTweet thisAlerts:
@hyperionXSMar 06.2012 — before session_start() put this line

error_reporting(E_ALL ^ E_NOTICE)
Copy linkTweet thisAlerts:
@jag420authorMar 06.2012 — i cant see any more errors...does this means it hides all the erros or that was problem ?
Copy linkTweet thisAlerts:
@hyperionXSMar 06.2012 — It doesn't hide errors, only notices. But make sure the script does what you need.

When there will be an error, it will be shown.
Copy linkTweet thisAlerts:
@jag420authorMar 06.2012 — ohh ok..yeh it working the way i want apart form total cost calculation doesnt seems to calculate correctly
×

Success!

Help @jag420 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 5.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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