All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Store Data In Database From HTML Form

Last Updated : Jul 1, 2023

How To Store Data In Database From HTML Form

In this tutorial we will show you the solution of how to store data in database from HTML form, for this, the primary and required material is an html form, xampp server, a database in phpmyadmin, basic knowledge of php.

Hence, we want to say that there are many ways to store data in database from html form. Let us understand how this can be done.

Step By Step Guide On How To Store Data In Database From HTML Form :-

Now, first we have to create form in html. No matter what input tags are used in form. After this there be a submit button to check and send data to database.

Rest job is of php, in which we connect database, store values in temporary variables, link variable to table columns and then insert data in database.

We hope that it may seems to complex, but don’t worry you will understand everything after this couple of lines that are in html.

<?php
$connect = mysqli_connect("localhost", "root", "", "as1") or die("Connection failed");
if(!empty($_POST['save']))
{
    $username=$_POST['name'];
    $usernmbr=$_POST[‘mobile_number’];
    $useremail=$_POST['email'];
    $usermarks=$_POST[‘total_marks’];
    $query = "insert into students_data(username, mob_nmbr, email, marks) values('$username', $usernmbr, $useremail, $usermarks)";
    if(mysqli_query($connect, $query))
    {
        echo "Record inserted successfully";
        echo "br"."RESPONSE will be send to you shortly";
    }
    else
    {
        echo"Soory, error found here";
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>
        Student’s form
    </title>
</head>
<body style="text-align: center;">
    <h1>
        Student’s form
    </h1>
    <h3>
        Fill the following details for next verification yourself
    </h3>
    <form action="" method="post">
       <label for="">
            Students Name
       </label>
       <input type="text" name="name" id="name">
       <br><br>
       <label for="">
           Students mobile number
       </label>
       <input type="number" name="mobile_number" id="m_nmbr">
       <br><br>
       <label for="">
           Students email
       </label>
       <input type="email" name="email" id="email">
       <br><br>
       <label for="">
  Student’s Total Marks
       </label>
       <input type="number" name="total_marks" id="t_marks">
       <br><br>
       <input type="submit" value="Click here to submit" name="save">
    </form>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page.
  4. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Here, we create a form for student. And under this form, we create some input for name, mobile number, email and total marks.
  6. And at last we create a button to submit form data. We hope that you understand this perfectly. Now, let us talk about php codes.
  7. Now, in php that is at beginning. Here we first create a connection between html and database.
  8. The database name used here is as1, whereas the table name is students_data. Now, here we make some variables to store data of forms in them. You can see how this can be done.
  9. Whereas the next step is of query which helps to make a connection between table columns and variables in which we store data.
  10. At last, an if statement is used here which is for store data with the help of mysqli_query. If data is stored in database then the codes in if gets executed. Whereas if there is some error then else part will be executed.
  11. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

In conclusion, here we can say that with the help of this article we are able to store data in database from an html form. This is a very simple and certified way to store data in database from html.

I hope this tutorial on how to store data in database from HTML form helps you.