[mysql]同一个表中插入多个窗体-只有最后一种形式插入

标签: jQuery MySQL PHP
发布时间: 2017/4/9 20:15:35
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我在我的网站有 2 形式。第一种形式是静态的 (一般信息)。第二个一 (项目信息) 可以复制 (与 jQuery) 如果人们想要插入多个项目。如果提交了超过 1 个项目,只有最后一个项目被插入到数据库中。这是不是它应该工作的。应提交所有项目窗体。窗体看起来像这样 (有 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:

首先,所有所有的由 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
}
赞助商