All TalkersCode Topics

Follow TalkersCode On Social Media

How To Insert Multiple Rows In MySQL Using PHP Form

Last Updated : Jul 1, 2023

How To Insert Multiple Rows In MySQL Using PHP Form

In this tutorial we will show you the solution of how to insert multiple rows in MySQL using PHP form, mainly, there are many ways with the help of which you are able to insert multiple rows in MySQL.

But here we are going to create a form and insert multiple rows in database from that form. Let us understand this with the help of codes.

Step By Step Guide On How To Insert Multiple Rows In Mysql Using PHP Form :-

As, before starting this we think that you already know how to establish connection databases connection and how to create a form with php.

If not then don’t worry in this php series we also come in touch with establishment of connection. We hope that our this article may help you. Now, let us start our code.

    $connect = mysqli_connect("localhost", "root", "", "as1") or die("Connection Failed");
    if (!empty($_POST['button']))
        $postusername = $_POST['user_name'];
        $postage = $_POST['age'];
        $query="insert into agedata(Username, Age) values('$postusername', $postage)";
            echo"Record Inserted successfully";
            echo"Ohh! sorry error found";
        input from form to database
<body style="text-align: center;">
    <form method="POST" action="#">
        <label for="">
            Enter your id here
        <input type="text" name="user_name" id="user_name">
        <label for="">
            Enter your age here
        <input type="number" name="age" id="age">
        <input type="submit" value="Submit this form" name="button">
  1. Now, here let us discuss codes. Here, we first create a form with the help of html. And we think that you know the basic structure of html with how to create a form inside html. If not there are some articles on our page that help you to understand html concepts.
  2. Now also, we have to create an empty database. And a table inside database. Here, in this example we create a database having name as1 and inside database we create a table having name age data.
  3. Inside this we save the name of user and their age also.
  4. Now, our next step is to write code in php to access database. As our previous articles, we establish connection with database with help of $connect and if connection is not established then connection failed gets printed.
  5. In next step, if our button is not empty means when we click on button then it gets or store data from inputs to variables and then a query runs.
  6. In query age data is our table name and after that we give columns name where we want to store data. And then values which we want to store inside table are inside values and their data gets from the variables inside values.
  7. At last if everything is ok then our mysqli_query will run and if it run with no error then a message record inserted successfully will printed else Ohh! Sorry error found will be printed.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to insert multiple rows in MySQL using php form. I hope this tutorial on how to insert multiple rows in MySQL using PHP form helps you.