0

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 .

here is my code

    <?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>
4
  • There is nothing inherent to a sql database that prevents you from adding records with with same customer id but different other field values Commented Jul 20, 2018 at 19:14
  • 1
    Can you post the structure of the customer table? Commented Jul 20, 2018 at 19:20
  • You want a UNQIUE constraint? Maybe also see ON DUPLICATE KEY UPDATE / INSERT? Commented Jul 20, 2018 at 19:21
  • thank you all guys , answer of barmer worked for me , thanks Commented Jul 21, 2018 at 5:09

1 Answer 1

4

Give the repeating fields array-style names:

<input type="text" name="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
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you this code fully worked for me , but when I give a message that data has been added it is also showing many time
Don't put that message in the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.