kpetsche20 Posted October 3, 2008 Share Posted October 3, 2008 Hello, I am making a checkout page for a shopping cart and I need for the quantities to be update able with only 1 submit button. I got it to work, but it only edits 1 value in the database, if other products are added to the table, the values do not update. Here's the code below. <?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['submit']) { mysql_query("UPDATE cart SET qty = '".$_POST['qty']."' WHERE id = '".$_POST['id']."'") or die(mysql_error()); echo "qty updated"; } ?> <form method="POST"> <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'] ?> Qty: <input type="text" name="qty" value="<?php echo $data['qty'] ?>"/> <input type="hidden" name="id" value="<?php echo $data['id']; ?>" /> <?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> <input type="submit" name="submit" value="submit" /> </form> <br> Link to comment https://forums.phpfreaks.com/topic/126872-php-update-problem/ Share on other sites More sharing options...
ranjuvs Posted October 3, 2008 Share Posted October 3, 2008 You need to loop the Update query for each items selected. Now it does only for the first item. Regards Ranju Link to comment https://forums.phpfreaks.com/topic/126872-php-update-problem/#findComment-656215 Share on other sites More sharing options...
kpetsche20 Posted October 3, 2008 Author Share Posted October 3, 2008 You need to loop the Update query for each items selected. Now it does only for the first item. Regards Ranju I put the update code into a while loop, but still only 1 value is getting inserted. The form tags are outside the loop and the textbox and hidden quantity value are inside the loop, not sure if that makes any difference. Anyway here is the loop i'm using the for update query, still only 1 value gets updated. if($_POST['submit']) { $sqhh = mysql_query("SELECT * FROM cart WHERE id = '".$_POST['id']."'") or die(mysql_error()); while($data4 = mysql_fetch_array($sqhh)) { mysql_query("UPDATE cart SET qty = '".$_POST['qty']."' WHERE id = '".$_POST['id']."'") or die(mysql_error()); } echo "qty updated"; } Link to comment https://forums.phpfreaks.com/topic/126872-php-update-problem/#findComment-656236 Share on other sites More sharing options...
ranjuvs Posted October 3, 2008 Share Posted October 3, 2008 no you need to loop using the posted value Link to comment https://forums.phpfreaks.com/topic/126872-php-update-problem/#findComment-656281 Share on other sites More sharing options...
kpetsche20 Posted October 3, 2008 Author Share Posted October 3, 2008 only 1 value get's inserted, it's not updating them all.. Link to comment https://forums.phpfreaks.com/topic/126872-php-update-problem/#findComment-656458 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.