为什么只有一部分价值才能进入我的数据库

 收藏

我有一个用于食品订购系统的代码,并将数量值输入到数据库中。我制作了可折叠的按钮来划分每个类别。问题只是给出输出值的最后一个类别(例如:我有3个类别,分别是汉堡,薯条和意大利面。当我输入1个芝士汉堡和2个意大利面时,输出值变成2个芝士汉堡和2个意大利面。当我输入仅输入1个芝士汉堡,由于意大利面的值为0,因此输出值变为0个芝士汉堡。请注意,芝士汉堡和意大利面都位于各自类别的第一行。 这些是我的输入代码

<?php include('header.php'); ?>
<body>
<?php include('navbar.php'); ?>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.collapsible {
  background-color: brown;
  color: white;
  cursor: pointer;
  padding: 18px;
  width: 100%;
  border-color: grey;
  text-align: left;
  outline: none;
  font-size: 15px;
}

.active, .collapsible:hover {
  background-color: #555;
}

.content {
  padding: 0 18px;
  display: none;
  overflow: hidden;
  background-color: #f1f1f1;
}
</style>

<div class="container">
    <h1 class="page-header text-center">ORDER</h1>
    <form method="POST" action="purchase.php">
        <?php
            $sql_category=$sql="select categoryid, catname from category order by categoryid asc";
            $query_category=$conn->query($sql_category);
            // while($row_category=$query_category->fetch_array()){
            while($row_category = $query_category->fetch_assoc()){
        ?>
            <button type="button" class="collapsible"><?php echo $row_category['catname']; ?></button>
            <div class="content">
                <table class="table table-striped table-bordered">
                    <thead>
                        <th class="text-center"><input type="checkbox" id="checkAll"></th>
                        <th>Category</th>
                        <th>Product Name</th>
                        <th>Price</th>
                        <th>Quantity</th>
                    </thead>
                    <tbody>
                        <?php 
                            $sql="select * from product left join category on category.categoryid=product.categoryid where product.categoryid=";
                            $sql.= $row_category['categoryid'];
                            $query=$conn->query($sql);
                            $iterate=0;
                            while($row=$query->fetch_array()){
                                ?>
                                <tr>
                                    <td class="text-center"><input type="checkbox" value="<?php echo $row['productid']; ?>||<?php echo $iterate; ?>" name="productid[]" style=""></td>
                                    <td><?php echo $row['catname']; ?></td>
                                    <td><?php echo $row['productname']; ?></td>
                                    <td class="text-right">&#x20A8; <?php echo number_format($row['price'], 2); ?></td>
                                    <td><input type="text" class="form-control" name="quantity_<?php echo $iterate; ?>"></td>
                                </tr>
                                <?php
                                $iterate++;
                            }
                        ?>
                    </tbody>
                </table>
            </div>
            <?php
            }
            ?>

        <div class="row">
            <div class="col-md-3">
                <input type="text" name="customer" class="form-control" placeholder="Customer Name" required>
            </div>
            <div class="col-md-2" style="margin-left:-20px;">
                <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Save</button>
            </div>
        </div>
    </form>
</div>
<script>
var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
  coll[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var content = this.nextElementSibling;
    if (content.style.display === "block") {
      content.style.display = "none";
    } else {
      content.style.display = "block";
    }
  });
}
</script>
<script type="text/javascript">
    $(document).ready(function(){
        $("#checkAll").click(function(){
            $('input:checkbox').not(this).prop('checked', this.checked);
        });
    });
</script>
</body>
</html>

这些是我的输出代码

<?php
    include('conn.php');
    if(isset($_POST['productid'])){

        $customer=$_POST['customer'];
        $sql="insert into purchase (customer, date_purchase) values ('$customer', NOW())";
        $conn->query($sql);
        $pid=$conn->insert_id;

        $total=0;

        foreach($_POST['productid'] as $product):
        $proinfo=explode("||",$product);
        $productid=$proinfo[0];
        $iterate=$proinfo[1];
        $sql="select * from product where productid='$productid'";
        $query=$conn->query($sql);
        $row=$query->fetch_array();

        if (isset($_POST['quantity_'.$iterate])){
            $subt=$row['price']*$_POST['quantity_'.$iterate];
            $total+=$subt;

            $sql="insert into purchase_detail (purchaseid, productid, quantity, note) values ('$pid', '$productid', '".$_POST['quantity_'.$iterate]."', '".$_POST['note_'.$iterate]."')";
            $conn->query($sql);
        }
        endforeach;

        $sql="update purchase set total='$total' where purchaseid='$pid'";
        $conn->query($sql);
        header('location:order_print.php');     
    }
    else{
        ?>
        <script>
            window.alert('Please select a product');
            window.location.href='order.php';
        </script>
        <?php
    }
?>
回复