Member-only story
Insert Multiple Rows Into MySQL With PHP Using Foreach Arrays
In this tutorial, we are going to learn how to insert multiple rows into MySQL with PHP using foreach arrays. To insert multiple form data, we need to generate multiple form rows and we will do it by using click function in jquery to generate the rows on clicking the button multiple times, and on the other hand, we will use the remove() function to remove the desired row.

Generating multiple rows on clicking Add new row button using jquery before submitting the form
- When we click add new row button the first time, then we will get the length of the row by using the class of the field ‘SL No’ from the class attribute of SL No field.
- Now, we will increase the length of the rows by adding 1 with parseInt(1) and store in a variable i.
- Now, we will add the next row using the append() function in jquery and use the length variable i with all the remaining fields of the form.
- We will also remove the added rows with the help of remove function as mentioned in the code given below.
Submitting the multiple form fields in the PHP scripts
- Here, we will receive the submitted rows using the field sl no.
- Now, we will check the total rows submitted in the for loop with the help of count() function.
- We will store the remaining fields in the array variable of the for loop and insert the field values in the table of the database.
Here I am using a table name ‘student’ to insert the student information.
Also Read, How to append data to the dropdown list using jquery ajax and php
DDL information of the table ‘student’
— — — — — — — — — — –
CREATE TABLE `student` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`student_name` varchar(225) COLLATE…