Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
action in shopping cart needs index, but how?
Old 09-21-2009, 07:53 AM action in shopping cart needs index, but how?
Daan's Avatar
Junior Talker

Posts: 2
Name: Daan Schuuring
Trades: 0
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.

PHP Code:
<?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($totalCost2"."",");
}

?>
Thank you already for your help,
Daan.

EDIT: 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.

Last edited by Daan; 09-21-2009 at 07:57 AM..
Daan is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-21-2009, 10:08 AM Re: action in shopping cart needs index, but how?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
and is there a querystring parameter of "action" in the URL?
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-21-2009, 12:12 PM Re: action in shopping cart needs index, but how?
Daan's Avatar
Junior Talker

Posts: 2
Name: Daan Schuuring
Trades: 0
yes, i just post this piece of code. I don't get the problem when I click on add item. I only get the error when I go directly to the cart.php .

PHP Code:
echo "<a href='cart.php?action=add_item&id=".$row["productId"]."'>Add Item</a><br>"
Daan is offline
Reply With Quote
View Public Profile
 
Old 09-21-2009, 12:18 PM Re: action in shopping cart needs index, but how?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
as you would, because there is NO parameter in the URL
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to action in shopping cart needs index, but how?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.14122 seconds with 12 queries