kpetsche20 Posted October 4, 2008 Share Posted October 4, 2008 I have made a form display the quantities of products added to a shopping cart database table. What happens is only 1 value updates, if there are more then 1 product none of the other quantities get updated upon submitting the form. <?php include("../config.php"); include("../classes/admin.class.php"); include("../classes/balance.class.php"); ?> <?php if($_SESSION['id'] == false) { die(); } ?> Shopping Cart <br> <br> <?php include("../classes/balance.class.php"); $balance = new balance(); include("../classes/products.class.php"); $products = new products(); if($_GET['delete']) { mysql_query("DELETE FROM cart WHERE id = '".$_GET['delete']."'") or die(mysql_error()); echo "Product removed"; } if($_POST['qty']) { mysql_query("UPDATE cart SET qty = '".$_POST['qty']."' WHERE order_id = '".$_POST['id']."' AND pid = '".$_POST['pid']."'") or die(mysql_error()); echo "qty updated"; } ?> <table width="98%" border="0" cellspacing="0" cellpadding="4"> <tr> <td width="15%"> </td> <td width="15%">Name</td> <td colspan="2">Price</td> </tr> <?php $sql = mysql_query("SELECT * FROM cart WHERE userid = '".$_SESSION['id']."' AND order_id = '".$products->get_order_number()."'") or die(mysql_error()); while($data = mysql_fetch_array($sql)) { ?> <tr> <td><?php echo $data['pid'] ?> <form method="POST" id="sc" name="sc" action="shoppingcart.php"> Qty: <input type="text" name="qty" value="<?php echo $data['qty'] ?>"/> <input type="hidden" name="id" value="<?php echo $products->get_order_number(); ?>" /> <input type="hidden" name="pid" value="<?php echo $data['pid'] ?>" /> </form> <?php ?> </td> <td><?php echo $data['name'] ?></td> <td width="19%">$<?php echo $data['price'] ?></td> <td width="51%"><a href="index.php?p=shoppingcart&delete=<?php echo $data['id'] ?>">Delete</a></td> </tr> <?php } ?> <tr> <td colspan="2"><div align="right">Total:</div></td> <td colspan="2">$<?php echo $balance->shopping_cart('price', $_SESSION['id']); ?></td> </tr> <tr> <td colspan="2"> </td> <td colspan="2"><a href="index.php?p=checkout">Checkout</a></td> </tr> </table> <img src="../images/update.png" onclick="javascript: document.getElementById('sc').submit()" /> <br> Link to comment https://forums.phpfreaks.com/topic/126988-problem-with-phpmysql-update-in-a-loop/ Share on other sites More sharing options...
JasonLewis Posted October 4, 2008 Share Posted October 4, 2008 Bad coding in there, mainly your HTML. You have a loop that creates many forms, each with the same ID. You will need to change it so that all products are wrapped in the same form tag, then you will need to make all the field names arrays by putting [] at the end of them. Then you will need to change your update code a fair bit as well. Link to comment https://forums.phpfreaks.com/topic/126988-problem-with-phpmysql-update-in-a-loop/#findComment-656897 Share on other sites More sharing options...
kpetsche20 Posted October 4, 2008 Author Share Posted October 4, 2008 So you're saying to put the while loop inside the form tags? How do I put the form tags in a array? Link to comment https://forums.phpfreaks.com/topic/126988-problem-with-phpmysql-update-in-a-loop/#findComment-656898 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.