ckehrer Posted August 11, 2009 Share Posted August 11, 2009 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; } Quote Link to comment https://forums.phpfreaks.com/topic/169815-shopping-cart/ Share on other sites More sharing options...
kickstart Posted August 11, 2009 Share Posted August 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/169815-shopping-cart/#findComment-895917 Share on other sites More sharing options...
ckehrer Posted August 11, 2009 Author Share Posted August 11, 2009 Hi Keith, Thanks for the reply. What has me confused is every submit button when hovered over shows the correct id in the string to select. For example, the second button would have id=2, but on submit adds id=1 into database. Quote Link to comment https://forums.phpfreaks.com/topic/169815-shopping-cart/#findComment-896005 Share on other sites More sharing options...
kickstart Posted August 11, 2009 Share Posted August 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/169815-shopping-cart/#findComment-896021 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.