将多个表单插入到同一个表中 - 仅插入最后一个表单

时间:2016-06-23 08:46:59

标签: php jquery mysql forms

我的网站上有两个表格。第一种形式是静态的(一般信息)。如果人们想插入多个项目,可以复制第二个(项目信息)(使用jQuery)。如果提交了多个项目,则只会在数据库中插入最后一个项目。哪个不应该如何运作。应提交所有项目表格。表单看起来像这样(有2个项目):

enter image description here

PHP代码:

<?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);

}

?>

代码的jQuery部分:

 <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>

我做错了什么?期待您的回答。

1 个答案:

答案 0 :(得分:0)

首先,jQuery添加的所有输入都具有相同的名称(project_namehouse_amountpeople_typedelivery_date

然后,您的php代码无法处理多个新项目的插入。

重命名输入,使它们声明为数组

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

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

其中index将成为php数组中的关键字 - 您可以在案例中使用x变量

然后在你的php脚本中$_POST['projectName']将是一个你应该遍历的数组

另一种方法是命名输入:

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

然后在$_POST['projects']中你将拥有多维数组,你可以这样做:

foreach ($_POST['projects'] as $projectKey => $project) {
    $projectName = $project['projectName']; // don't forget to escape
    $houseAmount = $project['houseAmount'];
    // 
    // do insert to db
}