I am trying to store multiple value in dynamic created row in php form, everything is working fine but processing code only storing just first value into mysql table rather than all multi selected value. below is my code
<table class="table table-bordered">
<thead class="table-success" style="background-color: #3fbbc0;">
<tr>
<th width="10%"><center>Service</th>
<th width="10%"><center>Type</th>
<th width="10%"><center>Consultant</th>
<th width="10%"><center>Qty/Graf.</th>
<th width="10%"><center>Rate/Unit</th>
<th width="10%"><center>Discount</th>
<th width="10%"><center>Total</th>
<th width="10%"><center>Reff. By</th>
<th width="10%"><center>Asst By</th>
<th width="10%"><center>OT</th>
<th width="10%"><center>Remarks</th>
<th width="5%"></th>
<button type="button" class="btn btn-sm btn-success" onclick="BtnAdd()">Add Item</button>
</th>
</tr>
</thead>
<tbody id="TBody">
<tr id="TRow" class="d-none">
<td><Select class="country form-control text-end" name="country[]" id = "country" required>
<option value=""> Select Service</option>
<?php
include('db1.php');
$query = "select * from country";
// $query = mysqli_query($con, $qr);
$result = $con->query($query);
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?></option>
<?php
}
} ?> </select> </td>
<td><Select class="state form-control text-end" name="state[]" id = "state" required>
<option value="">select Type</option></select></td>
<td><Select class="city form-control text-end" name="city[]" id = "city" required onchange="GetDetail(this.closest('tr'))" >
<option value="">Select Cons</option></select></td>
<td><input type="text" class="qty form-control text-end" name="qty[]" id="ccc" onchange="Calc(this);" Required></td>
<td><input type="text" class="price form-control text-end" name="price1[]" id="ddd" onfocus="Calc(this);" readonly style="background-color: #3fbbc0;"></td>
<td><input type="text" class="discunt form-control text-end" name="discunt[]" id="eee" onchange="Calc(this);" ></td>
<td><input type="text" class="tot4 form-control text-end" name="tot4[]" id="fff" oninput="Calc(this);" Required readonly style="background-color: #3fbbc0;"></td>
<td><Select class="form-control text-end" name="tech1[]" id="ggg" Required onfocus="Calc(this);">
<option value="">Select Reff</option>
<?php
include('db.php');
$sql = mysqli_query($con,"SELECT * FROM reff");
while($row=mysqli_fetch_array($sql))
{
echo '<option value="'.$row['reffered'].'">'.$row['reffered'].'</option>';
} ?>
</select></td>
<td><Select class="form-control text-end" name="docname[]" id="iii" required onfocus="Calc(this);">
<option value="">Select Asst</option>
<?php
include('db.php');
$sql = mysqli_query($con,"SELECT * FROM tech1");
while($row=mysqli_fetch_array($sql))
{
echo '<option value="'.$row['techname'].'">'.$row['techname'].'</option>';
} ?> </select></td>
<td><Select class="form-control text-end chosen-select" name="docname1[][]" id="iii" multiple required onfocus="Calc(this);">
<option value="">Select OT</option>
<?php
include('db.php');
$sql = mysqli_query($con,"SELECT * FROM mmmach");
while($row=mysqli_fetch_array($sql))
{
echo '<option value="'.$row['mach1'].'">'.$row['mach1'].'</option>';
} ?> </select></td>
<td><input type="text" class="form-control text-end" name="remarks3[]" id="zzz" >
<input type="hidden" class="form-control text-end mop" name="remarks4[]" id="zzz9" ><input type="hidden" class="zzz1 form-control text-end" name="zzz1[]" id="zzz1" ><input type="hidden" class="zzz2 form-control text-end" name="zzz2[]" id="zzz2" ><input type="hidden" class="zzz3 form-control text-end" name="zzz3[]" id="zzz3" ><input type="hidden" class="zzz4 form-control text-end" name="zzz4[]" id="zzz4" ><input type="hidden" class="zzz5 form-control text-end" name="zzz5[]" id="zzz5" ></td>
<td class="NoPrint"><button type="button" class="btn btn-success" style="line-height: 1;" onclick="BtnDel(this)">x</button></td>
</tr> </tbody> </table>
Processing code
<?php
if (isset($_POST['submit'])) {
// getting all values from the HTML form
}
// database details
$host = "localhost";
$username = "oot";
$password = "agghhj";
$dbname = "hmis";
// creating a connection
$con = mysqli_connect($host, $username, $password, $dbname);
// to ensure that the connection is made
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
// Process each set of form inputs
for ($i = 0; $i < count($_POST['city']); $i++) {
// Check if docname1 is set and convert it to a comma-separated string
$docname1Array = isset($_POST['docname1']) ? $_POST['docname1'] : [];
$docname1 = isset($docname1Array[$i]) ? implode(',', (array) $docname1Array[$i]) : '';
$country = mysqli_real_escape_string($con, $_POST['country'][$i]);
$state = mysqli_real_escape_string($con, $_POST['state'][$i]);
$city = mysqli_real_escape_string($con, $_POST['city'][$i]);
$qty = mysqli_real_escape_string($con, $_POST['qty'][$i]);
$price1 = mysqli_real_escape_string($con, $_POST['price1'][$i]);
$tot4 = mysqli_real_escape_string($con, $_POST['tot4'][$i]);
$sqlInsertItem = "
INSERT INTO iap44 (country, state, city, qty, price1, tot4, docname1)
VALUES ('$country', '$state', '$city', '$qty', '$price1', '$tot4', '$docname1')";
$rs1 = mysqli_query($con, $sqlInsertItem);
if (!$rs1) {
echo "Error: " . mysqli_error($con);
}
}
echo "<pre>";
print_r($_POST);
echo "</pre>";
// close the connection
mysqli_close($con);
?>