Last insert id with two child table in php mysql

i have three mysql table iap3, iap4 and iap5. Iap3 is parents where as iap4 and iap 5 both child table with multiple records against one parents record. I have one php form as shown below. the problem which i am facing is that I generated last insert id from parent table (iap3) and want to insert it against every record in child table in both table (iap4 and iap5). but the problem which I am getting is that it successfully entered last id for both child (Iap4 and Iap5) and also show all child entries of iap4 table but its only stored first record in iap5 table rather than all the entered one (i needed last insert id to interlink parent table with child one )

<form method="POST" action="invoice.php" >
            <div class="card-body">      
             /* IAP3 table associated form fields */
                 <label>MR No:</label> <input type="text" name= "mrno"  id= "mrno" value="<?php echo $number1; ?>" class="form-control" Required>
        <label>Name:</label> <input type="text" name= "name1"  id= "name1" class="form-control" Required>                            
                    
            /* IAP4 table associated form fields */
                <table class="table table-bordered">
                    <thead class="table-success" style="background-color: #3fbbc0;">
                      <tr>
                        
                        <th width="15%"><center>Scode</th>
                        <th width="10%"><center>Docname</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><input type="text"  class="scode form-control text-end" name="scode[]" id = "hhh"></td>
      <td><input type="text"  class="docname form-control text-end" name="docname[]" id = "hhh1"></td>      
                         
  
                  <td class="NoPrint"><button type="button" class="btn btn-success"  style="line-height: 1;" onclick="BtnDel(this)">x</button></td>
            
                      </tr> </tbody> </table>           
      
                 
 **/* IAP5 table associated form fields */**
      <table class="table table-bordered">
                    <thead class="table-success" style="background-color: #3fbbc0;">
                      <tr>
                        
                        <th width="40%"><center>scode1</th>
                        <th width="40%"><center>docname1</th>
            <th width="40%"><center>Action</th>
                                               
                         <button type="button" class="btn btn-sm btn-success" onclick="BtnAdd1()">Add Item</button>                         
                        </th>

                      </tr>
                    </thead>
                    <tbody id="TBody1">
                      <tr id="TRow1" class="d-none">
                       
       <td><input type="text"  class="scode1 form-control text-end" name="scode1[]" id = "scode1" ></td>
       <td><input type = "text" class="form-control text-end" name="docname1[]" id="docname1" Required>                         
       <td class="NoPrint"><button type="button" class="btn btn-success"  style="line-height: 1;" onclick="BtnDel(this)">x</button></td>                    
        </tr></tbody></table></div> </div></div>        
    

<div class="col-lg-4"> <div class="form-group">
        <label></div></div>
 <div class="col-lg-4"> <div class="form-group"><center>
        <input type="submit" name="submit" id="submit" value="Submit"  class="btn btn-success submit_btn invoice-save-btm"> 
            

</form>
    



 </body>
</html>


<script type="text/javascript">

function GetPrint()
{
    /*For Print*/
    window.print();
}

function BtnAdd()
{
    /*Add Button*/
    var v = $("#TRow").clone().appendTo("#TBody") ;
    $(v).find("input").val('');
    $(v).find("input").autocomplete({
        source: 'backend-script.php'  
    });
    $(v).removeClass("d-none");
    $(v).find("th").first().html($('#TBody tr').length - 1);
}

function BtnDel(v)
{
    /*Delete Button*/
       $(v).parent().parent().remove(); 
       GetTotal();

        $("#TBody").find("tr").each(
        function(index)
        {
           $(this).find("th").first().html(index);
        }

       );
}


 
        function BtnAdd1()
{
    /*Add Button*/
    var v = $("#TRow1").clone().appendTo("#TBody1") ;
    $(v).find("input").val('');
    $(v).find("input").autocomplete({
        source: 'backend-script.php'  
    });
    $(v).removeClass("d-none");
    $(v).find("th").first().html($('#TBody tr').length - 1);
}

function BtnDel(v)
{
    /*Delete Button*/
       $(v).parent().parent().remove(); 
       GetTotal();

        $("#TBody1").find("tr").each(
        function(index)
        {
           $(this).find("th").first().html(index);
        }

       );
}  </script>
                  
    here is invoice .php the
<?php

if(isset($_POST['submit']))
{
// getting all values from the HTML form   

        $mrno = $_POST['mrno'];
        $name1 = $_POST['name1'];
       $scode =  $_POST['scode'];
        $docname =  $_POST['docname'];

        $scode1 =  $_POST['scode1'];
        $docname1 =  $_POST['docname1'];     
    
}


 // database details
    $host = "localhost";
    $username = "root";
    $password = "";
    $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());
    }

    // using sql to create a data entry query
$sqlInsert = "INSERT INTO iap3 (mrno, name1)
             VALUES ('$mrno', '$name1')";       
           $rs = mysqli_query($con, $sqlInsert);

    $last_id = mysqli_insert_id($con);
  
for ($i = 0; $i < count($scode); $i++) {
            $sqlInsertItem = "
            INSERT INTO iap4(pid, scode, docname) 
            VALUES (' $last_id', '$scode[$i]',  '$docname[$i]')";           
            $rs1 = mysqli_query($con, $sqlInsertItem);
        }           

  for ($i = 0; $i < count($scode1); $i++) {
            $sqlInsertItem = "
            INSERT INTO iap5(pid1, scode1, docname1) 
            VALUES (' $last_id', '$scode1[$i]',  '$docname1[$i]')";         
            $rs1 = mysqli_query($con, $sqlInsertItem);
        } 
    // close connection
    mysqli_close($con);
?>

Aren’t you missing a 1 there?

and there…

so its front end issue due to which i only get one record in iap5 instead of multiple ???

Possibly. I’m not sure why the IAP4 code actually works; You should be required to wrap the variables in curly braces to make PHP process it correctly.

Does $scode1 actually contain all of the data its supposed to? If not, you’ve got a front end issue. var_dump($scode1) and take a look at it.

even after adding 1, problem still exist, it still onyl stored data for first record only in iap5.

So when you dumped the variable, what was in it?

i didnt dump yet, should i only dump for iap5 ?? or iap 4 as well

Doesnt really matter. All this is doing is pointing you at where the problem lies; if the variable contains many entries, and your code is only inserting one, your problem is either in PHP or the database. If it only contains one entry, your frontend is the problem, because PHP isnt receiving all of the data.

1 Like

i thnk there is problem in java script code where i am cloning dynamically created new rows. can you check both add button code for new rows in front end (iap4 and iap5 )

So when you dumped the variable, it only had 1 entry in it?

where i have to dump scode1 ??? inside for loop or in insert querry or on the top ?

Immediately after declaring it. Basically, I want to know what’s in $_POST[“scode1”].

how can u tell ?
i have 2 enteries on front end as follow "
scode1 doctname2
cash abc
card xyz

I declared it as follow and after duming its showing this output
$scode1 = $_POST[‘scode1’];
var_dump($scode1);

output screen after submitting form
array(1) { [0]=> string(4) “cash” }

Okay so it’s only getting one value. We can put the PHP and Database stuff to the side for now. The problem is definitively somewhere in the frontend.

Thing is, when i take your (partially fixed) code, stick it into Codepen, (and tell Codepen to load jQuery, and to remove the autocomplete), the form works as expected for me…

You sure you’ve shown us the code that is actually running on the page?

when i had only two table iap3 and iap4, its worked fine, but when i added third table it start creating issue. ok let me send u complete code for both front end and back end : front end :

<?php
// Initialize the session
session_start();
 
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
    header("location: login.php");
    exit;
}
?>

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Patient Bill</title> 

	<script src=
		"https://code.jquery.com/jquery-3.2.1.min.js">
	</script>

	<script src=
"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
		type="text/javascript">
	</script>
	
	<link rel="stylesheet" href=
"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

	<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
	</script>

   
        <!-- jQuery CDN -->
    <script src="https://code.jquery.com/jquery-3.6.2.slim.js" integrity="sha256-OflJKW8Z8amEUuCaflBZJ4GOg4+JnNh9JdVfoV+6biw=" crossorigin="anonymous"></script>
    
   

</head>
  <body>

<?php

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "hmis";
    $conn = mysqli_connect($servername,$username,$password,$dbname);
?>

<?php
$query = "SELECT mrno FROM iap3 ORDER BY mrno DESC";
$result = mysqli_query($conn,$query);
$row = mysqli_fetch_array($result);
$lastid = $row['mrno'];
if(empty($lastid))
{
    $number1 = "F-00001";
}
else
{
    $idd = str_replace("F-", "", $lastid);
    $id = str_pad($idd + 1, 5, 0, STR_PAD_LEFT);
    $number1 = 'F-'.$id;
}
?>

<?php
$query = "SELECT bilno FROM iap3 ORDER BY bilno DESC";
$result = mysqli_query($conn,$query);
$row = mysqli_fetch_array($result);
$lastid = $row['bilno'];
if(empty($lastid))
{
    $number = "B-00001";
}
else
{
    $idd = str_replace("B-", "", $lastid);
    $id = str_pad($idd + 1, 5, 0, STR_PAD_LEFT);
    $number = 'B-'.$id;
}
?>

    <div class="container ">
       <div class="panel panel-default">
 <div class="panel-heading" style="background-color: #3fbbc0;"><h4>Patient Bill</h4></div>   
              
             <div class="panel-body">
	<form method="POST" action="invoice.php"  target="_blank">
            <div class="card-body">

               
                            
                            
                        </div>
            
                       <div class="row"> <div class="col-lg-4"> <div class="form-group">
		<label>MR No:</label> <input type="text" name= "mrno"  id= "mrno" value="<?php echo $number1; ?>" class="form-control" Required>
		</div> </div> 
<div class="col-lg-4"> <div class="form-group">
		<label>Name:</label> <input type="text" name= "name1"  id= "name1" class="form-control" Required>
		</div> </div>
<div class="col-lg-4"> <div class="form-group">
		<label>Age :</label> <input type="text" name= "age1"  id= "age1" class="form-control">
		</div> </div></div>

<div class="row"> <div class="col-lg-4"> <div class="form-group">
				<label>Gender:</label> <Select name="gender1"  id='gender1'  class="form-control">

<option value="">Select Gender</option>
<?php
include('db.php');
$sql = mysqli_query($con,"SELECT * FROM gender");
while($row=mysqli_fetch_array($sql))
{
echo '<option value="'.$row['gen1'].'">'.$row['gen1'].'</option>';
} ?>



</select>
		</div> </div> 
<div class="col-lg-4"> <div class="form-group">
		<label>Mobile:</label> <input type="text" name= "mobno"  id= "mobno" class="form-control"  Required>
		</div> </div>
<div class="col-lg-4"> <div class="form-group">
		<label>Patient Catagory:</label> <Select name="pcatagry"  id='pcatagry' class="form-control">

<option value="Private">Private</option>
<?php
include('db.php');
$sql = mysqli_query($con,"SELECT * FROM pc");
while($row=mysqli_fetch_array($sql))
{
echo '<option value="'.$row['pc1'].'">'.$row['pc1'].'</option>';
} ?>



</select>
		</div> </div></div>

<div class="row"> <div class="col-lg-4"> <div class="form-group">
		<label>Department :</label> <Select name= "deprtmnt"  id= "deprtmnt" class="form-control">
<option value="OPD">OPD</option>
<?php
include('db.php');
$sql = mysqli_query($con,"SELECT * FROM maind");
while($row=mysqli_fetch_array($sql))
{
echo '<option value="'.$row['mainddd'].'">'.$row['mainddd'].'</option>';
} ?>



</select>
		</div> </div> 
<div class="col-lg-4"> <div class="form-group">
		<label>Bill No:</label> <input type="text" name= "bilno"  id= "bilno" class="form-control" value="<?php echo $number; ?>" readonly style="background-color: #3fbbc0;">
		</div> </div>
<div class="col-lg-4"> <div class="form-group">
		<label>Bill Date:</label> <input type="date" value = "<?php echo date("Y-m-d"); ?>" name= "bildate"  id= "bildate" class="form-control" readonly style="background-color: #3fbbc0;">
		</div> </div></div>



                <table class="table table-bordered">
                    <thead class="table-success" style="background-color: #3fbbc0;">
                      <tr>
                        
                        <th width="15%"><center>Service</th>
                        <th width="10%"><center>Mach No</th>
			<th width="10%"><center>Consultant</th> 
			<th width="5%"><center>Qty</th>                       
			<th width="10%"><center>Rate</th>
			<th width="10%"><center>Discount</th>			
			<th width="10%"><center>Total</th>
			<th width="15%"><center>Technition</th>
			<th width="20%"><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><input type="text"  class="scode form-control text-end" name="scode[]" id = "tutorial_name"   onchange="GetDetail(this.closest('tr'))"></td>

 
				
                         
   			 			<td><Select class="form-control text-end" name="machno[]" id="hhh" Required>
<option value="One">One</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><Select class="form-control text-end" name="docname[]" id="iii" required onfocus="Calc(this);">
<option value="">Select Consult</option>
<?php
include('db.php');
$sql = mysqli_query($con,"SELECT * FROM consultant");
while($row=mysqli_fetch_array($sql))
{
echo '<option value="'.$row['consultant_name'].'">'.$row['consultant_name'].'</option>';
} ?>
</select></td>
			<td><input type="text" class="qty form-control text-end" name="qty[]" id="ccc" onfocus="Calc(this);"></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"  readonly style="background-color: #3fbbc0;"></td>
			<td><Select class="form-control text-end" name="tech1[]" id="ggg" Required onfocus="Calc(this);">
<option value="">Select Technition</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><input type="text" class="form-control text-end" name="remarks3[]"  id="zzz" ><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" ></td>
                      
  
                  <td class="NoPrint"><button type="button" class="btn btn-success"  style="line-height: 1;" onclick="BtnDel(this)">x</button></td>
			
                      </tr>
                    </tbody>
 
        
                  </table>
<script>

		// onkeyup event will occur when the user
// release the key and calls the function
// assigned to this event
function GetDetail(row) {
  let str = row.querySelector(".scode").value;
  if (str.length == 0) {
      row.querySelector(".qty").value = "";
    row.querySelector(".price").value = "";
    row.querySelector(".discunt").value = "";
    row.querySelector(".tot4").value = "";
	row.querySelector(".zzz1").value = "";
	row.querySelector(".zzz2").value = "";
	row.querySelector(".zzz3").value = "";
    return;
  } else {
    // Creates a new XMLHttpRequest object
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {

      // Defines a function to be called when
      // the readyState property changes
      if (this.readyState == 4 &&
        this.status == 200) {

        // Typical action to be performed
        // when the document is ready
        var myObj = JSON.parse(this.responseText);

        // Returns the response data as a
        // string and store this array in
        // a variable assign the value
        // received to first name input field

       
        row.querySelector(".qty").value = myObj[0];
        row.querySelector(".price").value = myObj[1];
       row.querySelector(".discunt").value = myObj[2];	
       row.querySelector(".tot4").value = myObj[3];
	 row.querySelector(".zzz1").value = myObj[4];
 	row.querySelector(".zzz2").value = myObj[5];
 	row.querySelector(".zzz3").value = myObj[6];
      }
    };

    // xhttp.open("GET", "filename", true);
    xmlhttp.open("GET", "gfg.php?user_id=" + str, true);

    // Sends the request to the server
    xmlhttp.send();
  }
}
	</script>

                  
                    


                    </div>
                </div>
             
          

<div class="row"> 


<div class="col-lg-4"> <div class="form-group">
		<label>Total Bill:</label> <input type="text" name= "totbil"  id= "totbil" class="form-control" readonly style="background-color: #3fbbc0;">
		</div> </div>
<div class="col-lg-4"> <div class="form-group">
		<label>Advance Received:</label> <input type="text" name= "adv11"  id= "adv11" class="form-control"  onchange="GetTotal()" readonly style="background-color: #3fbbc0;">
		</div> </div> 
<div class="col-lg-4"> <div class="form-group">
		<label>Net Payable:</label> <input type="text" name= "netpayable1"  id= "netpayable1" class="form-control" readonly style="background-color: #3fbbc0;">
		</div> </div>

</div>




<div class="row"> 

<div class="col-lg-6"> <div class="form-group">

 <table class="table table-bordered">
                    <thead class="table-success" style="background-color: #3fbbc0;">
                      <tr>
                        
                        <th width="40%"><center>Payment Mode</th>
                        <th width="40%"><center>Payment Recieved</th>
			<th width="40%"><center>Action</th>
                                               
                         <button type="button" class="btn btn-sm btn-success" onclick="BtnAdd1()">Add Item</button>                         
                        </th>

                      </tr>
                    </thead>
                    <tbody id="TBody1">
                      <tr id="TRow1" class="d-none">
                       
       <td><input type="text"  class="scode1 form-control text-end" name="scode1[]" id = "scode1" ></td>
       <td><input type = "text" class="form-control text-end" name="machno1[]" id="machno1" Required>                         
       <td class="NoPrint"><button type="button" class="btn btn-success"  style="line-height: 1;" onclick="BtnDel(this)">x</button></td>		 			
        </tr></tbody></table></div> </div></div>        
	<div class="row"> 

<div class="col-lg-4"> <div class="form-group">
		<label>Total Amount Received:</label> <input type="text" name= "payrecved"  id= "payrecved" class="form-control"  onchange="GetTotal()" Required>
		</div> </div>
<div class="col-lg-4"> <div class="form-group">
		<label>Balance:</label> <input type="text" name= "netpay1"  id= "netpay1" class="form-control" >
</div> </div>
<div class="col-lg-4"> <div class="form-group">
		<label>Operator:</label><input type="text" name="abc" id="abc"  readonly class="form-control" value = "<?php echo htmlspecialchars($_SESSION["username"]); ?>"  style="background-color: #3fbbc0;">
		</div> </div> </div>



<div class="row">

<div class="col-lg-4"> <div class="form-group">
		<label></div></div>

<div class="col-lg-4"> <div class="form-group">
		<label></div></div>
 <div class="col-lg-4"> <div class="form-group"><center>
		<input type="submit" name="submit" id="submit" value="Submit"  class="btn btn-success submit_btn invoice-save-btm">	
			

</form>
    
 <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
 <script src="https://code.jquery.com/ui/1.12.1/jquery.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script type="text/javascript" src="frontend-script.js"></script>
 


    <!-- Bootstrap Bundle JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
 

 

</div>


 </body>
</html>


<script type="text/javascript">

function GetPrint()
{
    /*For Print*/
    window.print();
}

function BtnAdd()
{
    /*Add Button*/
    var v = $("#TRow").clone().appendTo("#TBody") ;
    $(v).find("input").val('');
    $(v).find("input").autocomplete({
        source: 'backend-script.php'  
    });
    $(v).removeClass("d-none");
    $(v).find("th").first().html($('#TBody tr').length - 1);
}

function BtnDel(v)
{
    /*Delete Button*/
       $(v).parent().parent().remove(); 
       GetTotal();

        $("#TBody").find("tr").each(
        function(index)
        {
           $(this).find("th").first().html(index);
        }

       );
}





function Calc(v)
{
    /*Detail Calculation Each Row*/
    var index = $(v).parent().parent().index();
    
    var qqq1 = document.getElementsByName("qty[]")[index].value;
    var rrr1 = document.getElementsByName("price1[]")[index].value;
    var ddd1 = document.getElementsByName("discunt[]")[index].value;    
 	    var tot4  =  +qqq1 * +rrr1 - +ddd1 

    document.getElementsByName("tot4[]")[index].value = tot4;

    GetTotal();
}

function GetTotal()
{
    /*Footer Calculation*/   

    var sum=0;
    var amts =  document.getElementsByName("tot4[]");

    for (let index = 0; index < amts.length; index++)
    {
        var tot4 = amts[index].value;
        sum = +(sum) +  +(tot4) ; 
    }

    document.getElementById("totbil").value = sum;

    var gst =  document.getElementById("adv11").value;
    var net = +(sum) - +(gst);
    document.getElementById("netpayable1").value = net;

	var gst1 =  document.getElementById("bycard1").value;
	var gst2 =  document.getElementById("bycash1").value;

	var net3 = +(gst1) + +(gst2);	

        document.getElementById("payrecved").value = net3;
	
		var net4 = +(net) - +(net3);	
	document.getElementById("netpay1").value = net4;

}
		
                      
                    
 
        function BtnAdd1()
{
    /*Add Button*/
    var v = $("#TRow1").clone().appendTo("#TBody1") ;
    $(v).find("input").val('');
    $(v).find("input").autocomplete({
        source: 'backend-script.php'  
    });
    $(v).removeClass("d-none");
    $(v).find("th").first().html($('#TBody1 tr').length - 1);
}

function BtnDel(v)
{
    /*Delete Button*/
       $(v).parent().parent().remove(); 
       GetTotal();

        $("#TBody1").find("tr").each(
        function(index)
        {
           $(this).find("th").first().html(index);
        }

       );
}
                  
		
		</script> 

back end code as follow
<?php

if(isset($_POST['submit']))
{
// getting all values from the HTML form
   
        $mrno = $_POST['mrno'];
        $name1 = $_POST['name1'];
        $age1 = $_POST['age1'];
        $mobno = $_POST['mobno'];
        $gender1 = $_POST['gender1'];     
        $pcatagry = $_POST['pcatagry'];
        $deprtmnt = $_POST['deprtmnt'];
        $bilno = $_POST['bilno'];
        $bildate = $_POST['bildate'];
     
        $totbil = $_POST['totbil'];
        $adv11 = $_POST['adv11'];
        $netpayable1 = $_POST['netpayable1'];
	
	
        $payrecved = $_POST['payrecved'];
        $netpay1 = $_POST['netpay1'];
        $abc = $_POST['abc']; 

        $scode =  $_POST['scode'];
       	$docname =  $_POST['docname'];
        $machno =  $_POST['machno'];
	 $qty =  $_POST['qty'];
        $price1 =  $_POST['price1'];
	 $discunt =  $_POST['discunt'];
        $tot4 =  $_POST['tot4'];
        $tech1=  $_POST['tech1'];
  	$remarks3=  $_POST['remarks3'];
	$zzz1=  $_POST['zzz1'];
	$zzz2=  $_POST['zzz2'];
	$zzz3=  $_POST['zzz3'];	


      $scode1 = $_POST['scode1'];
var_dump($scode1);
	$machno1 = $_POST['machno1'];
}


 // database details
    $host = "localhost";
    $username = "root";
    $password = "";
    $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());
    }

    // using sql to create a data entry query
$sqlInsert = "INSERT INTO iap3 (mrno, name1, age1, mobno, gender1,  pcatagry, deprtmnt, bilno, bildate,   totbil, adv11, netpayable1,  payrecved, netpay1, uname1) 
			 VALUES ('$mrno', '$name1', '$age1', '$mobno', '$gender1', '$pcatagry', '$deprtmnt', '$bilno', '$bildate',   '$totbil', '$adv11', '$netpayable1',  '$payrecved', '$netpay1', '$abc')";		
		   $rs = mysqli_query($con, $sqlInsert);

    $last_id = mysqli_insert_id($con);
  
for ($i = 0; $i < count($scode); $i++) {
			$sqlInsertItem = "
			INSERT INTO iap4(pid, scode, docname, machno, qty, price1, discunt, tot4, tech1, remarks3, zzz1, zzz2, zzz3) 
			VALUES ('$last_id', '$scode[$i]',  '$docname[$i]', '$machno[$i]', '$qty[$i]', '$price1[$i]', '$discunt[$i]', '$tot4[$i]', '$tech1[$i]', '$remarks3[$i]', '$zzz1[$i]', '$zzz2[$i]', '$zzz3[$i]')";			
			$rs1 = mysqli_query($con, $sqlInsertItem);
		}       	

  for ($i = 0; $i < count($scode1); $i++) {
			$sqlInsertItem1 = "
			INSERT INTO iap5(pid1, scode1, machno1) 
			VALUES ('$last_id', '$scode1[$i]',  '$machno1[$i]')";			
			$rs1 = mysqli_query($con, $sqlInsertItem1);
		} 
    // close connection
    mysqli_close($con);  ?> 

Okay, well, for starters, why are we loading jQuery 1.12.4, 1.12.1, 3.2.1, 3.4.1, and 3.6.2 all in the same page?

(I’m still looking, but that just glares out at me from the first few lines of code)

its for different purposes, i deleted iap4 table and guess what, it still showing just one row in iap 5. now there are only two table but still its not solving the issue. tel me one thing can i use one java script function to deynamically create rows for both iap4 and iap5 ?

Sorry but this is nonsense….