Jump to content

PHP Update Problem


kpetsche20

Recommended Posts

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

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

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.