action in shopping cart needs index, but how?
Posted: Mon Sep 21, 2009 7:32 am
Hello,
I've been working on a simple shopping cart and want to put the cart functions all in one file. Currently I am debugging here and there, but there is still one issue. Though I know what the problem is, I don't know exactly how to solve it.
When I run the script I get the following error:
Notice: Undefined index: action in C:\wamp\www\shoppingcart\cart.php on line 6
I need to do something like:
$action = some kind of variable here.
But I don't know what to put in there. Been searching everywhere but there is no real good answer to it.
I don't get the error of course when I click on a add product on my productlist and refer the header to my cart.php. Because I then use the action and get a variable out of it.
Thank you already for your help,
Daan.
I've been working on a simple shopping cart and want to put the cart functions all in one file. Currently I am debugging here and there, but there is still one issue. Though I know what the problem is, I don't know exactly how to solve it.
When I run the script I get the following error:
Notice: Undefined index: action in C:\wamp\www\shoppingcart\cart.php on line 6
I need to do something like:
$action = some kind of variable here.
But I don't know what to put in there. Been searching everywhere but there is no real good answer to it.
I don't get the error of course when I click on a add product on my productlist and refer the header to my cart.php. Because I then use the action and get a variable out of it.
Code: Select all
<?php
include 'session.php';
include 'configdb.php';
include 'connectdb.php';
switch($_GET["action"])
{
case "add_item":
{
AddProduct($_GET["id"]);
ShowCart();
break;
}
case "update_item":
{
UpdateProduct($_GET["id"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["id"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}
function AddProduct($productId) {
$id_session = session_id();
$result = mysql_query("select count(*) from cart where sessionId = '" . $id_session . "' and productId = $productId");
$row = mysql_fetch_row($result);
$numRows = $row[0];
if($numRows == 0)
{
// This item doesn't exist in the users cart,
// we will add it with an insert query
@mysql_query("insert into cart(sessionId, productId, qty) values('" . $id_session . "', $productId, 1)");
header("Location: productlist.php");
}
else
{
// This item already exists in the users cart,
// we will update it instead
UpdateProduct($productId);
}
}
function UpdateProduct($productId, $qty) {
$id_session = session_id();
@mysql_query("update cart set qty = qty + 1 where sessionId = '" . $id_session . "' and productId = $productId");
header("Location: productlist.php");
}
function ShowCart(){
$id_session = session_id();
$totalCost = 0;
$sql = "SELECT *
FROM cart
INNER JOIN products
ON cart.productId = products.productId
WHERE cart.sessionId = '" . $id_session . "'";
// open the table and print a header row.
if ($result=mysql_query($sql)) {
while ($row=mysql_fetch_array($result)) {
echo "".$row['productName']." ";
echo "".$row['productDesc']." ";
echo "".$row['productPrice']."<br>";
$totalCost += $row["qty"] * $row["productPrice"];
}
}
else {
echo "<!-- SQL Error ".mysql_error()." -->";
}
echo "Total: ";
echo number_format($totalCost, 2, ".", ",");
}
?>
Daan.