PHP Program To Insert Data Into MySQL Table Using HTML Form

This post gonna be very useful for those people who are interested to learn how to create a database connection using PHP with MySQL as well as how to Insert Data into MySQL Table Using PHP.
Before going to further steps I assume that you are familiar with MySQL and have some basic knowledge in a Database table and HTML form.

How to get HTML form data in PHP variable?

Above link will help you to have some clear idea on value passing from HTML form to PHP.

Suppose you have a table with these columns-


And ID column is auto_increment

You can create this table yourself or you can simply copy the create table SQL statement.

 `username` varchar(20) NOT NULL,
 `password` varchar(10) NOT NULL,
 `name` varchar(50) DEFAULT NULL,
 `email` varchar(30) DEFAULT NULL,
 `country` varchar(20) DEFAULT NULL,
 `age` int(5) DEFAULT NULL,
 `sex` varchar(6) DEFAULT NULL,


Here we gonna learn how to insert data into MySQL database table. So in order to do that the very first thing we need, is to make an HTML form where user gonna put the values to be inserted into Database Table.

<!DOCTYPE html>

            background-color: lavender;
            color: black;
        <title>Insert Data into MySQL Table Using PHP and HTML Form</title>
<h2>Insert Data Into The Database</h2>
        <form name="entry" method="post" action="create.php">
            User Name(Will be used for login):<br>
            <input type="text" name="username" required><br>
            Create Password:<br>
            <input type="password" name="pw" required><br>
            Full Name:<br>
            <input type="text" name="name"><br>
            Enter Email:<br>
            <input type="email" name="email" required><br>
            Country Name:<br>
            <input type="text" name="country"><br>
            Enter Age:<br>
            <input type="text" name="age"><br><br>
            Choose Sex:
            <select name="sex">
                <option value="Male">Male</option>
                <option value="Female">Female</option>
                <option value="Others">Others</option>
            <input type="submit" value="Save Data">

Save this file as index.php

PHP program to insert data into MySQL table using HTML form

$servername = "localhost";
$username = "root"; // database username, default Username for MySQL database is root
$password = ""; //database password, default password for mysql is null
$dbname = 'codespeedy'; //Database name
$u =$_POST['username'];
$p =$_POST['pw'];
$n =$_POST['name'];
$e =$_POST['email'];
$c =$_POST['country'];
$a =$_POST['age'];
$s =$_POST['sex'];

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

$sql = "INSERT INTO data (username, password, name, email, country, age, sex) // here data is my table name. you can use your own 
VALUES ('$u', '$p', '$n', '$e', '$c', $a, '$s')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
    header( "refresh:3;url=index.php" ); //this line used to redirect to the index.php page after 3 seconds
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;


Save this file as create.php


Remember we did not take input for ID column because it’s an auto_increment column so the Id will be generated automatically and will increment automatically.
To check if your data has been inserted or not you can use this query.




or you can check it in phpMyAdmin
I will recommend you to check this link if you have confusion in value passing from HTML page to PHP

How to get HTML form data in PHP variable?


Leave a Reply

Your email address will not be published. Required fields are marked *