Jump to content

Shopping Cart


ckehrer

Recommended Posts

Thanks in advance.

 

I'm currently working on a shopping cart where I have the cart items dynamically populated to the form. Each item has it's own add to cart button, but the problem I'm having is when I select any of the individual buttons the database only inserts the first item ID. Here is some of my code.


<?php

$command = "SELECT productID,name,price FROM $table_1";
$result = mysql_query($command);
while($data = mysql_fetch_object($result)) { ?>

                        <td width="35%"><img src='<? echo $data->product_image; ?>' /></td>
		<td><input type='hidden' name='name[]' value='<? echo $data->name; ?>' /><? echo $data->name; ?></td>	
		<td><input type='hidden' name='price[]' value='<? echo $data->price; ?>' /><? echo $data->price; ?></td>
		<td><input type='hidden' name='productID[]' value='<? echo $data->productID;?>' /></td>
		<td><a href="?product=<? echo $data->name; ?>&item=<? echo $data->productID; ?>"><input type='submit' name='submit' value='Add' /></a></td>
		<td><a href='#'><input type='submit' name='submit' value='View' /></a></td>
                        <td><a href='#'><input type='submit' name='submit' value='Checkout' /></a></td></tr>
		<? 

						// on form submit add products to table cart_purchases

						switch ($_GET['submit']) {

							case 'Add':

								$productID = $data->productID;
								$name = $data->name;
								$price = $data->price;																	


								$command = sprintf("INSERT INTO $table_3 (purchaseID,productID,name,price) VALUES ('','%u','%s','%u')",						
								mysql_real_escape_string($productID),mysql_real_escape_string($name),mysql_real_escape_string($price));
											   
								$result = mysql_query($command);

									break;	
								}


Link to comment
https://forums.phpfreaks.com/topic/169815-shopping-cart/
Share on other sites

Hi

 

What you seem to be doing is looping through the returned rows from a table and inserting into a different table while also putting out rows onto the screen.

 

However you will land up with multiple submit buttons on the screen all with the same name (and several different names per row displayed). Irrespective of which submit button is clicked the form will be processed (and you do not check which one is checked).

 

A lot of changes are needed , but think more info is needed.

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/169815-shopping-cart/#findComment-895917
Share on other sites

Hi

 

There is nothing on the submit buttons to give an id although you do have the buttons within link tags, one of which does have the id.

 

For the insert you check $_GET['submit'] is set, but there are numerous copies of that and you have nothing to identify which one is which.

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/169815-shopping-cart/#findComment-896021
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.