Q:

Insert multiple forms into same table - only last form is inserted

I have 2 forms on my website. The first form is static (general information). The second one (project information) can be duplicated (with jQuery) if people want to insert multiple projects. If more than 1 project is submitted, only the last project is inserted in the database. Which is not how it should work. All project forms should be submitted. The form looks like this (with 2 projects):

PHP code: 
<?php include './includes/database.php';
if(isset($_POST['submit'])){
   $company_name = mysqli_real_escape_string($connection, $_POST['company_name']);
   $contact_name = mysqli_real_escape_string($connection, $_POST['contact_name']);
   $email_address = mysqli_real_escape_string($connection, $_POST['email_address']);
   $phone_number = mysqli_real_escape_string($connection, $_POST['phone_number']);
   $project_name = mysqli_real_escape_string($connection, $_POST['project_name']);
   $house_amount = mysqli_real_escape_string($connection, $_POST['house_amount']);
   $people_type = mysqli_real_escape_string($connection, $_POST['people_type']);
   $delivery_date = mysqli_real_escape_string($connection, $_POST['delivery_date']);

//Set date
$signup_date = date('Y-m-d', time());

//Lowercase email
$email_address = strtolower($email_address);

mysqli_autocommit($connection, false);

$flag = true;

    $query = "INSERT INTO developers_prospects (signup_date, company_name, contact_name, email_address, phone_number)
    VALUES ('$signup_date','$company_name','$contact_name','$email_address','$phone_number')";

    $result = mysqli_query($connection, $query);

    if (!$result) {
       $flag = false;
        echo "Error details: " . mysqli_error($connection) . ".";
    }

    $query1 = "INSERT INTO developers_prospects_projects2 (company_name, project_name, house_amount, people_type, delivery_date)

    VALUES ('$company_name','$project_name','$house_amount','$people_type','$delivery_date')";    

    $result = mysqli_query($connection, $query1);

    if (!$result) {
       $flag = false;
        echo "Error details: " . mysqli_error($connection) . ".";
    }

if ($flag) {
    mysqli_commit($connection);
    $success = "Bedankt $contact_name! We hebben je gegevens in goede orde ontvangen.";
    header ("Location: index.php?success=".urlencode($success));
} else {
    mysqli_rollback($connection);
    $error = "Oeps. Sorry $contact_name! Er ging iets mis.";
    header ("Location: index.php?error=".urlencode($error));  

} 

mysqli_close($connection);

}

?>

And the jQuery part of the code:
 <script>
        $(document).ready(function() {
            var max_fields      = 10; //maximum input boxes allowed
            var wrapper         = $(".developers-signup-form-container"); //Fields wrapper
            var add_button      = $(".developers-signup-form-add"); //Add button ID
            var x = 1; //initlal text box count

            $(add_button).click(function(e){ //on add input button click
                e.preventDefault();
                if(x < max_fields){ //max input box allowed
                    x++; //text box increment
                    $len= $(".developers-signup-form-container").children("div").length+1;
                    $(wrapper).append("<div class='developers-signup-form-wrapper'><a href='#' class='developers-signup-remove-field'>X Sluiten</a><label class='developers-signup-form-label'>Projectnaam:</label><input class='developers-signup-form-field' type='text' name='project_name' ><label class='developers-signup-form-label'>Aantal woningen:</label><input class='developers-signup-form-field' type='text' name='house_amount' ><label class='developers-signup-form-label'>Type bewoners:</label><input class='developers-signup-form-field' type='text' name='people_type' ><label class='developers-signup-form-label'>Gewenste afleverdatum:</label><input class='developers-signup-form-field' type='text' name='delivery_date' ></div>");} });

            $(wrapper).on("click",".developers-signup-remove-field", function(e){ //user click on remove text
                e.preventDefault(); $(this).parent('div').remove(); x--;
            }) });
    </script>

What am I doing wrong? Looking forward to your answers.

A:

First of all all of your inputs that are added by jQuery have the same name (project_name, house_amount, people_type, delivery_date)
Then again your php code doesn't handle multiple insert of new projects.
Rename the inputs so they are declared as arrays
'<input class="..." name="projectName[]"/>'

or
'...<input class="..." name="projectName[' + index + ']"/>...'

where index will be a key in php array - you possibly could use x variable in your case
Then in your php script $_POST['projectName'] will be an array which you should loop through
Another way would be to name inputs like this:
'<input class="..." name="projects[' + index + '][projectName]" />
<input class="..." name="projects[' + index + '][house_amount]" />'

Then in $_POST['projects'] you will have multidimensional array and you could do something like this:
foreach ($_POST['projects'] as $projectKey => $project) {
    $projectName = $project['projectName']; // don't forget to escape
    $houseAmount = $project['houseAmount'];
    // 
    // do insert to db
}

