PHP 7 Script to Insert HTML5 Form Data to MySQL Database Using $_GET and $_POST Methods Full Project For Beginners

  • Post author:
  • Post category:PHP
  • Post comments:0 Comments


Welcome folks today in this blog post we will be inserting html5 form data to mysql database using $_GET and $_POST methods in php 7.All the full source code of the application is shown below.




Get Started




In order to get started you need to make an index.php file and copy paste the following code







$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

// Create connection
$conn = new mysqli($servername,
    $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: "
        . $conn->connect_error);

$sqlquery = "INSERT INTO table VALUES
    ('John', 'Doe', '')"

if ($conn->query($sql) === TRUE) {
    echo "record inserted successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;

    // collect value of input field
    $data = $_REQUEST['val1'];
    if (empty($data)) {
        echo "data is empty";
    } else {
        echo $data;
// Closing the connection.


<!DOCTYPE html>
<html lang="en">

    <title>GFG- Store Data</title>

        <h1>Storing Form data in Database</h1>

        <form action="insert.php" method="post">
                <label for="firstName">First Name:</label>
                <input type="text" name="first_name" id="firstName">

                <label for="lastName">Last Name:</label>
                <input type="text" name="last_name" id="lastName">

                <label for="Gender">Gender:</label>
                <input type="text" name="gender" id="Gender">

                <label for="Address">Address:</label>
                <input type="text" name="address" id="Address">

                <label for="emailAddress">Email Address:</label>
                <input type="text" name="email" id="emailAddress">

            <input type="submit" value="Submit">


        // servername => localhost
        // username => root
        // password => empty
        // database name => staff
        $conn = mysqli_connect("localhost", "root", "", "staff");
        // Check connection
        if($conn === false){
            die("ERROR: Could not connect. " 
                . mysqli_connect_error());
        // Taking all 5 values from the form data(input)
        $first_name =  $_REQUEST['first_name'];
        $last_name = $_REQUEST['last_name'];
        $gender =  $_REQUEST['gender'];
        $address = $_REQUEST['address'];
        $email = $_REQUEST['email'];
        // Performing insert query execution
        // here our table name is college
        $sql = "INSERT INTO college  VALUES ('$first_name', 
        if(mysqli_query($conn, $sql)){
            echo "<h3>data stored in a database successfully." 
                . " Please browse your localhost php my admin" 
                . " to view the updated data</h3>"; 
            echo nl2br("\n$first_name\n $last_name\n "
                . "$gender\n $address\n $email");
        } else{
            echo "ERROR: Hush! Sorry $sql. " 
                . mysqli_error($conn);
        // Close connection

Leave a Reply