All TalkersCode Topics

Follow TalkersCode On Social Media

Insert Multiple Rows Into MySQL With PHP Using Foreach Arrays

Last Updated : Jan 1, 2023

Insert Multiple Rows Into MySQL With PHP Using Foreach Arrays

In this tutorial we will show you the solution of insert multiple rows into MySQL with PHP using foreach arrays, there are many functions with help of which are able to insert multiple rows into the MySQL database.

Let us understand a few of them with help of the example below.

Step By Step Guide On Insert Multiple Rows Into Mysql With PHP Using Foreach Arrays :-

Now, the ways which we are going to understand in this series are:

  • With help of format() function
  • With help of getTimestamp() function
  • With help of strtotime() function

In this tutorial, we are going to understand the first way which is the format() whereas the rest will be discussed in the next session.

So, let us understand the use of the format() function with help of the codes given below. Before starting let us see the syntax of the format() function first.

        $name = $_POST['name'][$i];
        $phone_no = $_POST['phone_no'][$i];
        $age = $_POST['age'][$i];
        $date_of_birth = $_POST['date_of_birth'][$i];
        if($student_name!=='' && $phone_no!=='' && $age!=='' && $date_of_birth!==''){
    $query="INSERT INTO table_name(name,phone_no,age,date_of_birth)VALUES('$name','$phone_no','$age','$date_of_birth')";
mysqli_query($connection, $query);
            echo 'Error';
        echo "Submitted successfully";
<title> insert multiple rows into MySQL with PHP using foreach arrays </title>
<h1 align="center"> Insert multiple rows into MySQL with PHP using foreach arrays </h1>
    <form action="#" method="post">
          <label> Serial No:</label>
          <input type="text" sl" name="slno[]" id="slno" value="1" readonly="">
          <label for="Name">Name:</label>
          <input type="text" name="student_name[]" id="st_name" placeholder="Enter Name">
        <div class="col-sm-3">
         <label>Phone No:</label>
          <input type="text" name="phone_no[]" id="pn" placeholder="Enter Phone No">
          <input type="text" id="age" name="age[]" placeholder="Enter Age">
        <div class="col-sm-3">
            <input type="date" id="dob" name="date_of_birth[]"/>
        <div id="next"></div>
    <button type="submit" name="submit">Submit</button>
<script src="jquery-3.2.1.min.js"></script>
  1. As, here we see that we that in above example we show you an example in which HTML and PHP codes are used.
  2. Here, first of all, we create a basic structure of HTML, in which we use <!DOCTYPE html> which defines the type of document. And next one is our HTML tags. These tags are paired tags and all the data regarding HTML is written inside these tags.
  3. After we use our head tag which is again paired tag and contains the title and meta information of the webpage. The data written inside the head is not shown on the webpage.
  4. Now, next is the body which is the main tag of HTML. The data which we have written inside the body is shown on the webpage. Mostly all tags which are helpful to show data or information on the screen are written under the body tag.
  5. Now, as we see inside the body we write our PHP codes. These codes are started with <?PHP and end with ?>, these tags are known as basic PHP tags. Our PHP codes are always written under these interfaces. In our above example as we see inside this basic structure of PHP.
  6. Here, above we create a form and use foreach to insert data into the database. Here, above if the condition runs successfully then data is inserted into the database otherwise it will show an error message.

Conclusion :-

At last, in conclusion, here we can say that with the help of this article we can understand the date to the timestamp in PHP.

I hope this tutorial on insert multiple rows into MySQL with PHP using foreach arrays helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials