How To Insert Multiple Rows In MySQL Using PHP Form
Last Updated : Mar 11, 2024
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.
<?php $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)"; if(mysqli_query($connect,$query)) { echo"Record Inserted successfully"; } else { echo"Ohh! sorry error found"; } } ?> <html> <head> <title> input from form to database </title> </head> <body style="text-align: center;"> <form method="POST" action="#"> <label for=""> Enter your id here </label> <input type="text" name="user_name" id="user_name"> <br><br> <label for=""> Enter your age here </label> <input type="number" name="age" id="age"> <br><br> <input type="submit" value="Submit this form" name="button"> </form> </body> </html>
- 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.
- 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.
- Inside this we save the name of user and their age also.
- 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.
- 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.
- 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.
- 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.