I am having a list of items I got from a product table in the db.
Now when a client choses a particular category, all the items in that group are displayed and he just enters the quantity for each in a table and submits. Now my issue is that each each time he submits just the last item on the list, it submits to the order table in the database.
What could I be doing wrong.
<form method="post" action="function.php" >
<table>
<thead>
<tr>
<th>Products</th>
<th>Crate Price</th>
<th>Quantity</th>
<th>Total Amount</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM products ";
$select_products = mysqli_query($conn,$query);
while($row = mysqli_fetch_assoc($select_products)){
$order_product_id = $row['productID'];
$order_product_name = $row['productName'];
$order_product_crateprice =
$row['productCratePrice'];
echo "<tr>";
echo " <td><input type='hidden' name='order_product_id' value='$order_product_id'>$order_product_name </td>";
echo " <td><input type='hidden' name='order_product_crateprice'value='$order_product_crateprice'>$order_product_crateprice </td>";
echo " <td> <input type='number' name='qty'> </td>";
echo " <td> </td>";
echo "</tr>";
}
?>
</tbody>
</table>
<input class="form-control" name="ordername" id="ordername"
type="text" placeholder="Set Order Name" value="" />
<input class="form-control" name="empties" id="empties"
type="number" placeholder="Number of Empties" value="" />
<input class="form-control" id="cash" name="cash" type="number"
placeholder="Number of Cash" value="" />
<input class="form-control" id="credit" name="credit" type="number"
placeholder="Enter your Contact" value="" />
<input class="form-control" name="orderdate" id="orderdate"
type="Date">
<input type="hidden" name="retailer" id="retailer"
value="Undefined" >
<button type="submit" name="submit_order"
class="btn btn-primary btn-SM">
SUBMIT
</button>
</div>
</form>
///////////Add into orders function.php
function Add_order() {
global $conn;
if (isset($_POST['submit_order'])) {
$order_name = $_POST["ordername"];
$order_product = $_POST["order_product_id"];
$order_crate_price = $_POST["order_product_crateprice"];
$order_qty = $_POST["qty"];
$order_delievry_date = $_POST["orderdate"];
$order_empties = $_POST["empties"];
$order_cash = $_POST["cash"];
$order_credit = $_POST["credit"];
$order_retailer = $_POST["retailer"];
$query = "INSERT INTO orders(orderCode, orderProductID,
orderProductQty, orderCratePrice)
VALUES ( '$order_name', '$order_product', '$order_qty',
'$order_crate_price')";
$addproduct= mysqli_query($conn,$query);
if (!$addproduct) {
die("Insert Failed" . mysqli_error($conn));
}
$query = "INSERT INTO payments(paymentOrdercode,
paymentOrderRetailerID, paymentOrderCash,
paymentOrderEmpties, paymentOrderCredit, paymentOrderDate)
VALUES ( '$order_name', '$order_retailer', '$order_cash',
'$order_empties','$order_credit','$order_delievry_date')";
$addpayment= mysqli_query($conn,$query);
if (!$addpayment) {
die("Insert Failed" . mysqli_error($conn));
}
}
}
?>