垂直数组值水平存储到mysql表中的不同列

function insert()
{

    var detail = [];




      for (var i = 0 ; i<= arrayA.length ; i++)
     {
       
        detail.push(arrayA[i]);
        
     }
    

    // });
    

$.ajax({  
    url:'insert.php',  
    method:"POST",  
    data:{  details: detail,},  
      
    success:function(data){  
    //alert(html);
    
    }  
   });  


}

var arrayA = [];
function addvalues()

    
    $('[name=data]').each(function() {
     
    arrayA.push($(this).val());

    });
    
    
    alert("record enter");
}

I have pushed values from bootstrap form into array. Form entries are continuous like user fill the form and press add then after confirmation that values is added into array user again enter that form with different values and press add. after that user press submit to insert values into table. now form is like

    <input class="form-control col-md-7 col-xs-12" name ="data" type="text"  value="" id="name-input" required >
<input class="form-control col-md-7 col-xs-12" name ="data" type="number"  value="" id="father-input" required>   
<input class="form-control col-md-7 col-xs-12" name ="data" type="number"  value="" id="mother-input" required>
<input class="form-control col-md-7 col-xs-12" name ="data" type="number"  value="" id="age-input" required >
<input class="form-control col-md-7 col-xs-12" name ="data" type="number"  value="" id="blood-input" required >
<button type="button" id="add" onclick = addvalues();>Add</button>

and on pressing submit button all these values of an array insert into table

 $sql1="INSERT INTO tble1 (name, father_name ,Mother_name,  age, blood_group)VALUES ";
 for($i=0;$i<count($values1);$i++) {
$sql1 .= " ('".$values1[$i]."', '".$values1[$i]."', '".$values1[$i]."','".$values1[$i]."','".$values1[$i]."'),";
 }
 $sql1_trimmed = rtrim($sql1,',');

But, this query enter value at 0 in all field. I want to enter value 0 in first field 1 in second and so on.

</div>
$sql1="INSERT INTO tble1 (name, father_name ,Mother_name,  age, blood_group)VALUES ('";
for($i=0;$i<count($values1);$i++) {
   if($i == count($values1)-1){
        $sql1 .= $values1[$i]."')";
        return 0;
   }
   $sql1 .= $values1[$i]."', '";
}

But you shouldn't do insert in this way because it's dangerous it's called "sql injection"

You have to do it like this

$req = $bdd->prepare('INSERT INTO tble1(name, father_name ,Mother_name,  age, blood_group) VALUES(:name, :father_name , :Mother_name,  :age, :blood_group)');
$req->execute(array(
    'name' => $values1[0],
    'father_name' => $values1[1],
    'Mother_name' => $values1[2],
    'age' => $values1[3],
    'blood_group' => $values1[4]
    ));

If you have an array like this

$values=[name,...., name, father_name, Mother_name, age, blood_group];

then just do like this

$values=array_chunk($values, 5);

for($i=0;$i<count($values);$i++) {
    $values1 = $values[$i];
$req = $bdd->prepare('INSERT INTO tble1(name, father_name ,Mother_name,  age, blood_group) VALUES(:name, :father_name , :Mother_name,  :age, :blood_group)');
$req->execute(array(
    'name' => $values1[0],
    'father_name' => $values1[1],
    'Mother_name' => $values1[2],
    'age' => $values1[3],
    'blood_group' => $values1[4]
    ));
}

Example

You got an array like this from your form

$values=[name0, father_name0, Mother_name0, age0, blood_group0, name1, father_name1, Mother_name1, age1, blood_group1];

it contain two sets of data name0, father_name0, Mother_name0, age0, blood_group0 and name1, father_name1, Mother_name1, age1, blood_group1 it may contain n set of data but all of them have 5 values thats why I used

$values=array_chunk($values, 5);

it will split the array into n (in this example it will be 2 array) array

let's return to our example after doing $values=array_chunk($values, 5); values will be equal to

$values=[[name0, father_name0, Mother_name0, age0, blood_group0], [name1, father_name1, Mother_name1, age1, blood_group1]];

and using for loop we will loop over these sub arrays like this

for($i=0;$i<count($values);$i++) {
   $values1 = $values[$i];
   ....
}

For the JS code you are adding the same values multiple time do this instead

var arrayA = [];
function addvalues()

    var len = arrayA.length;
    for(let i=len; i<$('[name=data]').length; i++){
        if($('[name=data]')[i].val() || $('[name=data]')[i].val()==0){//if you aren't sure that these values can be null or not. Delete this line if you think that they can't be null.
           arrayA.push($('[name=data]')[i].val());
        }
     }

}

Also for the insert function do just this

    function insert(){
       $.ajax({  
             url:'insert.php',  
             method:"POST",  
             data:{  details: arrayA},  
             success:function(data){  
                             //alert(html);
                     }  
       });  
    }