好,所以我有一个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>
</head>
<body>
<div class="container">
<h1 class="page-header text-center">ORDER</h1>
<form method="POST" action="purchase.php">
<h3 class="page-header text-center">KARIMATA</h3>
<button type="button" class="collapsible">PATIN</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>
<th>Note</th>
</thead>
<tbody>
<?php
$sql="select * from product left join category on category.categoryid=product.categoryid where product.categoryid=1";
$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">IDR <?php echo number_format($row['price'], 2); ?></td>
<td><input type="text" class="form-control" name="quantity_<?php echo $iterate; ?>"></td>
<td><input type="text" class="form-control" name="note_<?php echo $iterate; ?>"></td>
</tr>
<?php
$iterate++;
}
?>
</tbody>
</table>
</div>
<button type="button" class="collapsible">GURAME</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>
<th>Note</th>
</thead>
<tbody>
<?php
$sql="select * from product left join category on category.categoryid=product.categoryid where product.categoryid=2";
$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">IDR <?php echo number_format($row['price'], 2); ?></td>
<td><input type="text" class="form-control" name="quantity_<?php echo $iterate; ?>"></td>
<td><input type="text" class="form-control" name="note_<?php echo $iterate; ?>"></td>
</tr>
<?php
$iterate++;
}
?>
</tbody>
</table>
</div>
<button type="button" class="collapsible">AYAM</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>
<th>Note</th>
</thead>
<tbody>
<?php
$sql="select * from product left join category on category.categoryid=product.categoryid where product.categoryid=3";
$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">IDR <?php echo number_format($row['price'], 2); ?></td>
<td><input type="text" class="form-control" name="quantity_<?php echo $iterate; ?>"></td>
<td><input type="text" class="form-control" name="note_<?php echo $iterate; ?>"></td>
</tr>
<?php
$iterate++;
}
?>
</tbody>
</table>
</div>
<button type="button" class="collapsible">DORI</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>
<th>Note</th>
</thead>
<tbody>
<?php
$sql="select * from product left join category on category.categoryid=product.categoryid where product.categoryid=4";
$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">IDR <?php echo number_format($row['price'], 2); ?></td>
<td><input type="text" class="form-control" name="quantity_<?php echo $iterate; ?>"></td>
<td><input type="text" class="form-control" name="note_<?php echo $iterate; ?>"></td>
</tr>
<?php
$iterate++;
}
?>
</tbody>
</table>
</div>
<button type="button" class="collapsible">UDANG</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>
<th>Note</th>
</thead>
<tbody>
<?php
$sql="select * from product left join category on category.categoryid=product.categoryid where product.categoryid=5";
$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">IDR <?php echo number_format($row['price'], 2); ?></td>
<td><input type="text" class="form-control" name="quantity_<?php echo $iterate; ?>"></td>
<td><input type="text" class="form-control" name="note_<?php echo $iterate; ?>"></td>
</tr>
<?php
$iterate++;
}
?>
</tbody>
</table>
</div>
<button type="button" class="collapsible">CUMI</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>
<th>Note</th>
</thead>
<tbody>
<?php
$sql="select * from product left join category on category.categoryid=product.categoryid where product.categoryid=6";
$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">IDR <?php echo number_format($row['price'], 2); ?></td>
<td><input type="text" class="form-control" name="quantity_<?php echo $iterate; ?>"></td>
<td><input type="text" class="form-control" name="note_<?php echo $iterate; ?>"></td>
</tr>
<?php
$iterate++;
}
?>
</tbody>
</table>
</div>
<br><br>
<div class="row">
<div class="col-md-3">
<input type="text" name="customer" class="form-control" placeholder="Nomor Meja" 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>
<br><br>
</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>
编辑:这是我在purchase.php上的后续代码
<?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
}
?>'''
您正在使用的按钮必须是submit
类型,因为只有submit
类型可以向前发送数据。