Update cart - mysql table update - while loop

2019-09-18 02:23发布

问题:

new guy here! I am building a custom shopping cart driven by mysql and i am trying to update my cart item by item in terms of quantity. It seems that i am doing something wrong because when i try to update the quantity it only update the last item. I am posting the code below. Any help would be appreciated. Thanks in advance.

1.cart.php:

    $sql = "select * from orders";
    $result = mysql_query($sql);
    $num = mysql_num_rows($result);
    echo "Στοιχεία: ".$num;
    ?>
    <form name="cart" method="post" action="updatecart.php">
      <table border="2">
        <tr>
          <td>Α/Α</td>
          <td>img_name</td>
          <td>Reprints</td>
          <td>Color</td>
        </tr>
        <?php
        if($num){
            while ($row = mysql_fetch_array($result)){
             ?>
                <tr>
                  <td><?php  echo $row['item_id']; ?></td>
                  <td><?php echo $row['img_name']; ?></td>
                  <td><input type="number" name="quantity" value="<?php echo $row['quantity']; ?>"></td>
                  <input type="hidden" name="item_id" value="<? echo $row['item_id']; ?>">
                  <td><?php echo $row['color']; ?></td>
                </tr>
            <?php
            }
        }

            ?>
      </table>
      <input type="submit" name="update" value="Update Cart" />
      <input type="button" name="2checkout" value="Proceed to Checkout" />
</form>

2.updatecart.php

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<?php
    $database_name = "vog";
    $conn = mysql_connect("localhost","root","toor");
    mysql_select_db($database_name);

    $num = 2; //na to ferw me session meta edw!
    if(isset($_POST['update'])){
        $item_id = $_POST['item_id'];
        $i=1;

        while($i<=$num){
            $item_id = $_POST['item_id'][$i];
            $quantity = $_POST['quantity'];
            $sql2 = "update orders SET quantity = '$quantity' where item_id = '$item_id' ";
            $result2 = mysql_query($sql2) or die ("Error in query: $result2");
            $i++;
        }
    }

    if(isset($result2)){
        header("Location:cart.php");
    }
?>

So far it updates just the last record.

回答1:

in #1.cart.php use the inputs as array:

<input type="number" name="quantity[<?php  echo $row['item_id']; ?>]" value="<?php echo $row['quantity']; ?>">
<input type="hidden" name="item_id[<?php  echo $row['item_id']; ?>]" value="<? echo $row['item_id']; ?>">

and in #2.updatecart.php: process it like

 foreach($_POST['item_id'] as $key => $id){

 $item_id = $id;
 $quantity = $_POST['quantity'][$key];
 $sql2 = "update orders SET quantity = '$quantity' where item_id = '$item_id' ";
 $result2 = mysql_query($sql2) or die ("Error in query: $result2");
 $i++;

 }


回答2:

Your problem is with the names of the fields in your HTML form:

<input type="number" name="quantity" value="<?php echo $row['quantity']; ?>">
<input type="hidden" name="item_id" value="<? echo $row['item_id']; ?>">

I think you meant to call them quantity[] and item_id[] instead, so they will and up as arrays in your $_POST variable later on, now they overwrite eachother, making $_POST['item_id'] only contain the last id in the database.



回答3:

You need to tell PHP that you're using an array for your submitted form items. The way to do this is to make the name of each input quantity[]. You can also place the item ID directly in the array as a key. In cart.php you can do this in your loop:

<input type="number" name="quantity[<?php echo $row['item_id']; ?>]" 
                     value="<?php echo $row['quantity']; ?>"/>

In effect, this will putput something like:

<input type="number" name="quantity[2]" value="1" />
<input type="number" name="quantity[4]" value="1" />
<input type="number" name="quantity[8]" value="2" />

i.e. 1 of item 2, 1 of item 4 and 2 of item 8.

Then, in updatecart.php you can read in the array and process it in a loop.

if(isset($_POST['update'])){ 
   foreach ($_POST['quantity'] as $item_id => $item_qty) {
       $item_id  = (int)$item_id;
       $item_qty = (int)$item_qty;

       $sql2 = "update orders SET quantity = '$item_qty' where item_id = '$item_id' ";
       mysql_query($sql2);
   }
}


标签: php mysql cart