如何将具有相同名称的许多Input数据插入数据库?

时间:2018-07-20 19:11:20

标签: javascript php arrays

你好朋友,我有一个表单字段,我想在其中插入数据,该数据包含四个字段,分别是customer_id,field_name1,field_name2,field_name3, 问题是我要插入具有许多输入但输入名称相同的数据,例如field_name1 field_name2 field_name3,首先没有输入,但是当我单击按钮添加伙伴详细信息时,这些输入将由javascript显示。 像field_name1,field_name2,field_name3可以重复十次,我想根据其中具有相同customer_id的数字在数据库中插入数据,但是字段名称不同,下面是我的代码,希望您理解。

这是我的代码

    <?php
$conn=mysqli_connect("localhost","root","","satya");
if(isset($_POST['submit'])){
    $customer_id=$_POST['customer_id'];
for ($ix=0; $ix<count($_POST['field_name1']); $ix++)
{
    $field_data = array(
        'field_name1' => $_POST['field_name1'][$ix],
        'field_name2' => $_POST['field_name2'][$ix],
        'field_name3' => $_POST['field_name3'][$ix],

    );
    $sql="INSERT INTO customer(customer_id,details1,details2,details3) VALUES('$customer_id','$field_name1','$field_name1','$field_name1')";
    $result=mysqli_query($conn,$sql);
    if($result){
        echo "data has been inserted";
    }
    else{
        echo "data could not be inserted";
    }
}
}
 ?>

<!DOCTYPE html>
<html>
<head>
    <title>test</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    </head>
<body>
    <form action="" method="post">
        <div class="form-group">
    <div class="row" style="margin-top: 20px">
        <div class="col-md-4" ></div>
    <div class="col-md-4">
        <label for="customer_id">Customer ID </label>
        <input type="text" class="form-control" name="customer_id" placeholder="customer_id">
    </div>
    <div class="col-md-4"></div>
       </div>
    <div class="row" style="margin-top: 20px;">
<div class="col-md-3">
        <label for="details1">details1 </label>
            </div>
    <div class="col-md-3">
        <label for="details2">details2</label>
        </div>
    <div class="col-md-3">
        <label for="details3">details3</label>
            </div>
            <div class="col-md-3">
                 <div>

        <a href="javascript:void(0);" class="add_partner btn btn-primary" title="Add Partner Details">Add Partner Details</a>
    </div>
</div>
    <div class="partner_wrapper" >

</div>
</div>
<div class="col-md-4"></div>
<div class="col-md-4">
        <button  type="submit" class="btn btn-primary" name="submit">Submit</button>
    </div>
    <div class="col-md-4"></div>
</form>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</body>
</html>



<script>
$(document).ready(function(){



    var maxField = 10; //Input fields increment limitation
    var addButton = $('.add_partner'); //Add button selector
    var wrapper = $('.partner_wrapper'); //Input field wrapper
    var fieldHTML = '<span class="row"><div class="col-md-3"><input type="text" class="form-control"  name="field_name1[]" value=""/></div><div class="col-md-3"><input type="text" class="form-control" name="field_name2[]" value=""/></div><div class="col-md-3"><input type="text" class="form-control" name="field_name3[]" value=""/></div><button type="button" href="javascript:void(0);" class="remove_button btn btn-primary" title="Remove field">Remove</button></span>'; //New input field html 
    var x = 1; //Initial field counter is 1
    $(addButton).click(function(){ //Once add button is clicked
        if(x < maxField){ //Check maximum number of input fields
            x++; //Increment field counter
            $(wrapper).append(fieldHTML); // Add field html
        }
    });
    $(wrapper).on('click', '.remove_button', function(e){ //Once remove button is clicked
        e.preventDefault();
        $(this).parent('span').remove(); //Remove field html
        x--; //Decrement field counter
    });

});
</script>

1 个答案:

答案 0 :(得分:4)

给出重复字段数组样式名称:

<input type="text" name="field_name1[]">

PHP将把输入收集到数组中,因此$_POST['field_name1']将是一个数组。然后,您可以遍历它们:

foreach ($_POST['field_name1'] AS $index => $field1) {
    $field2 = $_POST['field_name2'][$index];
    $field3 = $_POST['field_name3'][$index];

    // now you can insert all these values into the DB
}