如何将具有相同名称的许多Input插入数据到数据库中? [英] How do I insert Data Into database with many Input which has same name?

查看:48
本文介绍了如何将具有相同名称的许多Input插入数据到数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

hello friends I have a form field in which I want to insert data which has four field which are customer_id , field_name1 ,field_name2, field_name3 , The question is I want to insert data which has many inputs but same input name like field_name1 field_name2 field_name3 , firstly there is no input but when I click on a button add partner details then these inputs shows by javascript . like field_name1,field_name2,field_name3 ten times can repeat , I want to insert data in database according to there numbers with same customer_id , but different field name , below is my code , hope you understand it .

这是我的代码

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

推荐答案

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

Give the repeating fields array-style names:

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

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

PHP will collect the inputs into arrays, so $_POST['field_name1'] will be an array. Then you can loop over them:

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
}

这篇关于如何将具有相同名称的许多Input插入数据到数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆