如何使用php中的代码在数据库中插入动态表单字段的值

I have a problem in trying to store database values in their respective rows inside the database using dynamic form fields. I have provided here the screenshots of my codes and outputs.

This is my script in adding dynamic form fields in which the form is located in a separate file named load_work_experience_form.php

This is the html code for the form I have appended in the my script to add a dynamic form fields

This is the look of my dynamic form fields

This happens to be the wrong output inside the database in which data values do not store in their proper record. I am attempting to insert 2 records of work experience but it seems that it has created 4 records.

The source code for adding into the database is supplied below. Kindly help me in fixing this problem. Thanks. More power:

<!--ADD WORK EXPERIENCE TO DATABASE -->
  <?php
    require'../admin/php/db_connection.php';
    if(isset($_POST['update_profile']))
    {

      if (isset($_POST['employer'])) {
        foreach ( $_POST['employer'] as $value ) {
        $values = mysql_real_escape_string($value);
        $query = mysql_query("INSERT INTO tbl_work_exp (employer) VALUES ('$values')");

        }}

        if (isset($_POST['job_position'])) {
        foreach ( $_POST['job_position'] as $value ) {
        $values = mysql_real_escape_string($value);
        $query = mysql_query("INSERT INTO tbl_work_exp (job_position) VALUES ('$values')");

        }}
//some more codes here for Work From and To. This website does not accept alot of codes. But the codes here are just like the ones at the top.

    }
  ?>
<!--ADD WORK EXPERIENCE TO DATABASE -->

In the case that you have every time the same fields with dynamic rows:

<!--ADD WORK EXPERIENCE TO DATABASE -->
  <?php
    require'../admin/php/db_connection.php';
    if(isset($_POST['update_profile']))
    {
        if (isset($_POST['employer'])) {
            for ($i = 0, $nCount = count($_POST['employer']); $i < $nCount; $i++) {
                $employer = mysql_real_escape_string($_POST['employer'][$i]);
                $job_position = mysql_real_escape_string($_POST['job_position'][$i]);
                $query = mysql_query('INSERT INTO tbl_work_exp (´employer´, ´job_position´) VALUES (´' . $employer . '´, ´' . $job_position . '´)');
            }
        }
    }
  ?>
<!--ADD WORK EXPERIENCE TO DATABASE -->

You must add there the other fields also...

The problem of your logic is that you do for every field a insert but you need only one insert for one row.

And please don't use mysql library in php, it is better to use mysqli

Your problem is that you pass each value separately. I strongly suggest to change your HTML markup to group each value, but that's not the goal here. As for your current problem, this is a quick solution that can help you through. Picking up from your current code:

<?php
if (isset($_POST['update_profile'])) {
    $profileFields = array('employer', 'job_position');
    $profiles = array();

    foreach ($profileFields as $field)
    {
        if (isset($_POST[$field])) {
            foreach ($_POST[$field] as $key => $value) {
                if (!isset($profiles[$key])) {
                    $profiles[$key] = array();
                }
                $profiles[$key][$field] = mysql_real_escape_string($value);
            }
        }
    }

    foreach ($profiles as $profile) {
        $tableCols = implode(",", array_keys($profile));
        $profileValues = implode("','", array_values($profile));
        $insertQuery = "INSERT INTO tbl_work_exp ({$tableCols}) VALUES ('{$profileValues}')";
    }
}
?>

Give or take a few tweaks or special treatment for each field. This is very generic code just to give you a guide.

Hope this helps

<?php
if(isset($_POST['Button_name'])){
for($i=0; $i<count($_POST['employer_name']); $i++){
$query="INSERT INTO table_name(employer_name,employer_age) VALUES ('".$_POST['employer_name']."','".$_POST['employer_age']."')";
$result=mysql_query($query);}}
<?php 
$count_post_value = $_POST['first_name'] //this is value of text box //


for($i=0; $i<$count_post_value; $i++) 
{ 
if(trim($_POST["first_name"][$i] && $_POST["last_name"] && $_POST["remarks"]!= '')) 
{   

$sql = mysql_query("INSERT INTO Table_name(first_name,last_name) VALUES('".$_POST["first_name"][$i]."','".$_POST["last_name"][$i]."')"); 
if($sql) 
{
echo "<script>alert('Inserted Successfully');</script>";
}


else 
{
echo "<script>alert('ERROR');</script>";
}

}

}  

?>