在foreach循环php中存储数据

I have created a form that allows a user to enter data, they also have the option to dynamically add more text boxes using JavaScript to provide more information if necessary. e.g. First Name, Last Name, Course. A user may only have one course or they may have many. For every course a user includes I want to be able to store an entire new row in the database. Is there a way I can do this?

Welcome to StackOverflow :)

You can do this for sure, but you need to pass every dataset to php. There you can store the data.

<input type="text" name="name1"><input type="text" name="gender1">
<input type="text" name="name2"><input type="text" name="gender2">

And write it to the db in php

for($i = 1; isset($_POST['name' . $i]); $i++)
    {
        $name = $_POST['name' . $i];
        $gender = $_POST['gender' . $i];
        // update DB with input values
    }

After that you have several new rows inserted.

Alternatively you could create an array for all data:

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

In PHP, you have to do following

$cnt = count($_POST['name']);
for($i=0;$i<$cnt;$i++){
   // do any update with database
}