in reply to Re: Duplicate items in cart do not appear
in thread Duplicate items in cart do not appear

Yes, I'm aware of that. What I need to do is store the entries into the cart indexed by item_id, which distinguishes a product from one color to another.

When I try to bring in item_id with this statement of code:
$clref = $dbh->prepare ("SELECT color FROM item WHERE prod_id = ".$ref +->{product_id}); $clref->execute(); while (my $color = $clref->fetchrow_hashref()) { push @{$ref->{color}}, $color->{color}; }
And, then carry everything over to my Add Item subroutine, I receive Shopping Cart is Empty message.

Now, here is what I did to the code to try to bring in the item_id and which later produces an empty shopping cart message.
$clref = $dbh->prepare ("SELECT item_id, color FROM item WHERE prod_id + = ".$ref->{product_id}); $clref->execute(); while (my $color, $item_id = $clref->fetchrow_hashref() +) { push @{$ref->{color}, {$ref->{item_id}}, $color->{color +}, $item_id->{item_id}; }

I'm trying to create a $ref->{item_id} so it is the same as all the other variables. Then, I'm trying to pass the $ref->{item_id} to the add_item subroutine.

It is quite apparent that I'm goofing up somewhere. I just don't know where.
Here's also what I've done to the add_item subroutine...
sub add_item { my ($dbh, $cart_ref, $item_id, $qty, $color) = @_; # If the item isn't already in the cart, look it up from the datab +ase # and store it in the cart as a new entry with a quantity of zero. if (!exists ($cart_ref->{$item_id})) { my $sth = $dbh->prepare ("SELECT * FROM item, catalog_pet WHER +E catalog_pet.product_id = item.prod_id AND catalog_pet.product_id = +?"); $sth->execute ($item_id); my $item_ref = $sth->fetchrow_hashref (); $sth->finish (); return if !defined ($item_ref); # this shouldn't happen... $cart_ref->{$item_id} = {}; # create new entry, indexed by +item ID $cart_ref->{$item_id}->{description} = $item_ref->{description +}; $cart_ref->{$item_id}->{price} = $item_ref->{price}; $cart_ref->{$item_id}->{qty} = 1; } $cart_ref->{$item_id}->{qty} = $qty; $cart_ref->{$item_id}->{color} = $color; }

You can see that I'm trying to store the cart entries by item_id. This attempt of coding produces an empty shopping cart message.

Replies are listed 'Best First'.
Re: Re: Re: Duplicate items in cart do not appear
by perrin (Chancellor) on Feb 24, 2003 at 18:42 UTC
    Your code:
    $clref = $dbh->prepare ("SELECT item_id, color FROM item WHERE prod_id + = ".$ref->{product_id}); $clref->execute(); while (my $color, $item_id = $clref->fetchrow_hashref() ) { push @{$ref->{color}, {$ref->{item_id}}, $color->{color}, $ite +m_id->{item_id}; }
    has a number of problems. First, you really should always use bind variables with DBI. If you don't, you'll have to escape the variables you put into the SQL yourself in order to prevent big security holes.

    Next, while (my $color, $item_id = $clref->fetchrow_hashref() does not make $item_id a lexical ("my") variable. It's a global. If you had use strict and warnings on, it would tell you about that. Also, it's not assigning what you want to $color or $item_id. You're just fetching the hashref that represents a row into $color. I can't really tell what you were trying to do with this code.

    Your add_item sub needs to use product_id and color in the SQL statement. Otherwise you will get multiple rows back. It's still doing a "select *" too.