You can use the below solution to get an idea, This can be enhance further.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.0/css/bootstrap-datepicker.css"
rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.0/js/bootstrap-datepicker.js"></script>
<form name="registration" id="myForm" action="" method="post" class="form-inline">
<div class="form-row">
<div class="form-group col-md-4">
<label class="sr-only" for="inlineFormInput">Distributor Name</label>
<select id="distributor_name" class="form-control" required>
<option value="">Select a Distributor:</option>
<option value="1 ">NATIONAL</option>
<option value="2 ">Sunny Enterprises</option>
<option value="3 ">Sajjad Enterprises</option>
<option value="4 ">Hassan Traders</option>
</select></div>
<div class="form-group col-md-4">
<input class="date-own form-control" style="width: 300px;" type="text" placeholder="Select Month" id="date"
required>
</div>
<table id="example">
<thead class="thead-inverse">
<tr>
<th>#</th>
<th>Product Name</th>
<th>NSP</th>
<th>Current Sales</th>
<th>Closing Balance</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row"><input type="text" id='pro_id' name="pro_name[]" size=4 value=175 disabled></th>
<td><input type="text" name="pro_name[]" value=AMBIEN 10 MG disabled></td>
<td><input type="text" class=nsp name="nsp[]" size=4 value=230 disabled></td>
<td><input type="number" class="curr_sales" size="5" name="curr_sales[]" value="0" required></td>
<td><input type="number" class="close_bal" size="5" name="close_bal[]" value="0" required></td>
</tr>
<th scope="row"><input type="text" id='pro_id' name="pro_name[]" size=4 value=176 disabled></th>
<td><input type="text" name="pro_name[]" value=AZISOFT 250MG disabled></td>
<td><input type="text" class=nsp name="nsp[]" size=4 value=191 disabled></td>
<td><input type="number" class="curr_sales" size="5" name="curr_sales[]" value="0" required></td>
<td><input type="number" class="close_bal" size="5" name="close_bal[]" value="0" required></td>
</tr>
<th scope="row"><input type="text" id='pro_id' name="pro_name[]" size=4 value=177 disabled></th>
<td><input type="text" name="pro_name[]" value=AZISOFT 500MG disabled></td>
<td><input type="text" class=nsp name="nsp[]" size=4 value=191 disabled></td>
<td><input type="number" class="curr_sales" size="5" name="curr_sales[]" value="0" required></td>
<td><input type="number" class="close_bal" size="5" name="close_bal[]" value="0" required></td>
</tr>
</tbody>
<tfoot>
<tr>
<th></th>
<th></th>
<th></th>
<th>
<div class="input-group">
<span class="input-group-addon">Total Current Sales</span>
<input type="number" name="amount" size="5" id='totalCS' readonly class="form-control">
</div>
</th>
<th>
<div class="input-group">
<span class="input-group-addon">Total Closing Balance</span>
<input type="number" name="amount" size="5" id='totalCB' readonly class="form-control">
</div>
</th>
</tr>
</tfoot>
</table>
<button type="submit" id="save" name="save" class="btn btn-primary">Save</button>
</div>
</form>
<script type="text/javascript">
$('.date-own').datepicker({
// minViewMode: 1,
format: 'yyyy-mm-dd',
autoclose: true
});
</script>
<div id="congrats"></div>
Notice that in all the input text name as []
to hold the multiple values for the field.
<?php
$con = mysqli_connect("localhost", "root", "", "del");
//Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$dbValues = [];
$distributor_name = $_POST["distributor_name"];
$date = $_POST["date"];
$totalCS = $_POST["totalCS"];
$totalCB = $_POST["totalCB"];
$currSales = $_POST["curr_sales"] ?? [];
$closeBal = $_POST["close_bal"] ?? [];
$nsp = $_POST["nsp"] ?? [];
$proIds = $_POST["pro_id"] ?? [];
$currSalesCount = count($currSales);//Assumed the current sales count is equal to other variables
//validate the count of all above array is same
for ($i = 0; $i < $currSalesCount; $i++) {
//If you want to add some sql injection you can do it here
$dbValues[] = "('{$distributor_name}','{$date}','{$currSales[$i]}','{$closeBal[$i]}','{$totalCS}','{$totalCB}','{$nsp[$i]}','$proIds[$i]')";
}
$strDbValues = implode(',',$dbValues);
//No need to bind params directory execute this bulk query
$stmt = $con->prepare("INSERT INTO `orders`(`d_id`, `order_date`, `current-sales`, `closing-balance`, `total_cs`, `total_cb`, `nsp`, `pro_id`) VALUES $strDbValues");
if ($stmt->execute()) {
echo 'Succefully Submitted';
};
$stmt->close();
solved How to insert multiple data to database using php? [closed]