How To Connect Html Form To MySQL Database Using PHP
Last Updated : Mar 11, 2024
In this tutorial we will show you the solution of how to connect HTML form to MySQL database using PHP in last tutorial, we understand about rules of php and in series of html, we already covered how to make a form.
So, before starting if you have any issue or problem regarding them then you can visit to our previous articles. Let’s start this article now.
Step By Step Guide On How To Connect HTML Form To MySQL Database Using PHP :-
Now, we have to create a database under phpmyadmin first and also to create a table under the database.
Here, below we show you some codes and later we will discuss these in deep.
Here, we did not create any html page, we will only show you code in php to show you how to connect html form with database.
<?php $connect=mysqli_connect("localhost","root","","myProject") or die(" Connection Failed"); $name= $_REQUEST[‘nm’]; $marks= $_REQUEST[‘mk’]; if(isset($_REQUEST[‘sBTN’])) { $query="insert into students(name, marks) values(‘$name’, $marks)"; if(mysqli_query($connect,$query)) { echo" WE INSERTED YOUR RECORD SUCCESSFULLY"; } else { echo" WE ARE SORRY, BUT YOUR RECORD IS NOT INSERTED"; } } ?>
- First, here we already said that we did not use any html code here. But we here show you how you are able to connect html form with database and this can be done with the help of php.
- First, in html we create a form and inside form there are two inputs for text and marks with name nm, mk respectively. And a submit button with name sBTN. Now, let’s understand rest article with the help of this html form.
- Now, let us understand the codes. First here we create opening and closing tags of php that are <?php ?>. Where <?php is opening tag of php and ?> is closing tag. All the contents of php are always written inside these tags.
- After this inside php tags, we create a variable named connect and make a connection with database :- $connect=mysqli_connect("localhost","root","","myProject") or die(" Connection Failed");
- Here, we use inline query of php than is mysqli_connect to make a connection with database.
- Under this first we use server name, then user name, then password used and at last database name. If due to some issue we are not able to make a connection then die will execute and connection failed displayed on browser screen. In this my database name is myProject.
- Here, we get the input name and marks from user and stored them in variable named as $name, $marks respectively.
- Now, is set function is used on button to check whether the data is filled on not.
- Whereas in next step we use $query, this is used here to insert data into database or table to show you that connection is established between html and database.
- Inside this, students is the table name that we create under database as1, which is already discussed. And after this name, marks are columns of table students, whereas john, 65 are values which we want to insert in table.
- At last we create if and use mysqli_query under this with values $connect, $query. This is, if we want to enter some data in database through html.
- Here, if everything is right. Then WE INSERT YOUR RECORD SUCCESSFULLY this line goes printed. If not then else part will be executed. We hope that you understand the article properly.
Conclusion :-
In conclusion, here we can say that after this article you are able to make a connection between html form and database with the help of MySQL and data gets also saved after click on submit button.
I hope this tutorial on how to connect HTML form to MySQL database using PHP helps you.