Forms when using PHP: Update Basket

Hi,

I have written code for a basket page. I am using Switch functions for:

add item - adds to basket and also checks if already in basket - if in basket already increases quantity.

remove item - button on basket page to delete that product from the basket.

update item - If a customer wants to change the quantity.

I have a problem with the update item. This is the code suggested to me:


function UpdateItem($ID, $quantity){

//It executes a simple UPDATE SQL query against the cart table, updating the quantity of one specific item. The cookieId //field is used to match the users session ID to that particular product, making sure that the quantity is only updated for //that item and the current user:


@mysql_query("update cart set quantity = quantity + '$quantity' where cookieid = '" . GetCartId() . "' and productid = $ID");

I cannot get it to work. I thought I might need to use a form to submit the quantity that the user has entered into the ‘quantity’ box. If the new quantity entered into the box can be submitted using PHP then i would prefer that.

How do I use the quantity entered by the customer to get the quantity updated?!?

It might be that the code above is correct but I do not have the right code on the basket page to use that code. I have a text box on the basket page but I am not entirely sure how to initiate the code above.

Matt.

I’m not sure exactly how you’re doing this, but it sounds messy.

The way I would suggest doing this is to build a php class to be your basket. The basket class will have properties (arrays) for quantitiy and prices. Both arrays would have the product ID as the key and one array will store the quantities for each item and the other array would store the prices for each item.

The basket class would have methods to add/delete/edit items and quantities and to calculate the total value of the basket.

When a user clicks something or enters a value in a text box to modify the contents of the cart, the appropriate class method is called to perform the task. Now whether you use forms or not is up to you, but the cart has to be maintained server side.

PHP code is executed server side and values are entered in a text box client side so obviously php can’t take a value from a text box.

You have to send the value in the text box to the php script on the server. You can do that with AJAX or append the value to the php script’s url via a query string or submit the value via a form.

but my question is can it be done with PHP or must it be a form?

There is a text box on the basket page. The customer could delete quantity 1 and write in 3. When they click update basket can this be updated using PHP or must it be done with a form!?!

If it cannot be done with PHP what is the code above suggesting? The bit I do not think PHP can do is take a value from a text box!?

Matt.

As suggested to you in this thread, echo out the query to see what is actually being run and post it if you can’t see any error.