PHP 7 Script to Insert Data into MySQL Table Using HTML5 Form in Browser Full Project For Beginners

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

 

Welcome folks today in this blog post we will be inserting data into mysql database using html5 form in browser using php 7.All the full source code of the application is given below.

 

 

 

Get Started

 

 

 

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

 

 

table.php

 

 

CREATE TABLE `data` (
 `id` int(10) NOT NULL AUTO_INCREMENT,
 `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,
 PRIMARY KEY (`id`)
);

 

 

Now make a index.php file which will insert the data to the above table

 

 

index.php

 

 

<!DOCTYPE html>

<html>
    <style>
        body{
            background-color: lavender;
            color: black;
           
        }
    </style>
    
        <title>Insert Data into MySQL Table Using PHP and HTML Form</title>
        
    
    <body>
<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>
            </select><br>
            <br>
            <input type="submit" value="Save Data">
        </form>
     </body>
</html>

<?php
$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;
}

$conn->close();
?>

Leave a Reply