In this tutorial, we are going to insert form data into MYSQL database which is submitted through HTML form.
HTML Form : First we will create a HTML form to take the user input.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<html> <head> <title>Insert Form Data In MYSQL Database Using PHP</title> </head> <body> <form action="insert.php" method="post"> Name : <input type="text" name="username"> </br> Email : <input type="text" name="email"> </br> <input type="submit" value="Insert"> </form> </form> </body> </html> |
The method of the form is of type POST because we have to submit this form when the user clicks on the insert button. The two user inputs are username and email, the input type is a text. After click on the Insert button the form should be submitted to our PHP file, for that we have to mention the PHP file name in the action property of thee form, insert.php is the file name.
PHP Code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php $con = mysqli_connect('127.0.0.1','root',''); if(!$con) { echo 'Not Connected To Server'; } if(!mysqli_select_db($con, 'tutorial')) { echo 'Database Not Selected'; } $Name = $_POST['username']; $Email = $_POST['email']; $sql = "INSERT INTO person (Name, Email) Values ('$Name','$Email')"; if(!mysqli_query($con,$sql)) { echo 'Not Inserted'; } else { echo 'Inserted'; } header("refresh:2; url=index.html"); ?> |
You can also watch the video tutorial.