wordpress使用生成的新行向表中添加数据

In wordpress I have a form like this

<form id="data-form" action="">
    <table id="user-data">
        <tr>
            <td>Name</td>
            <td>Email</td>
        </tr>
        <tr>
            <td><input type="text" name="name"></td>
            <td><input type="text" name="email"></td>
        </tr>
    </table>
    <button type="submit" value="submit" name="submit">Submit</button>
</form>

So when I am doing click on submit button it is inserting data to the table like this

global $wpdb;
if ( isset($_POST['submit']) ) {
  $wpdb->insert(
    'fl_user_data', 
    array(
      'id' => '',
      'name' => $_POST['name'],
      'email' => $_POST['email'])
  );
  echo 'success';
}

and it is doing insert the values to the database. Now I have used a button inside the form called as add row. So the button adds the row to the table. Now when I click the button it adds a row to the table like this

<a href="#" id="add-row">+Add Row</a>
<script>
  $('body').on('click', '#add-row', function(e) {
    var Html = $('<tr><td><input type="text" name="name"></td><td><input type="text" name="email"></td></tr>');
    $('table#user-data').append(Html);
  });
</script>

This one is adding a row when I click the add row button. So when after all the fields have been filled with the new rows and I click on submit button then it is inserting only one row values. So can someone tell me how to insert the rows of data in the database table in rows. Any help and suggestions will be really appreciable. Thanks

Because the fields you are adding all have the same name (name, email) then each field is just overwriting the next.

What you need to do, is every time you add a field make the name unique in a known way. So for example name1, email1; name2, email2; name3, email3 and so on. Then in your insert script, you need to cycle through and add them one at a time.

A better way might be to have a hidden field called something like "row_count" that you increment each time a row is added, that way on the PHP side you just use that number provided to loop through without having to figure out how many rows you have.

Don't forget to sanitise all input etc.