PHP is one of the best and most used programming languages in the current time. HTML stands for a hypertext markup language. HTML designs the structure of the website. You can not connect HTML without PHP programming and the MYSQL database. PHP stands for Hypertext preprocessor. It's used to create functionality for the website. We use PHP to connect simple HTML pages to the MYSQL database. I would like to suggest understanding the MYSQL database. MYSQL is used to create a database structure.
How to connect HTML pages with the database using PHP?
The HTML page can be connected with the database using PHP programming language and MYSQL queries. You have understood the MYSQL queries before connecting the HTML page to the database using PHP.
Step 1- First of all install the xampp and wamp server on your computer. Xampp and wamp are local servers. Here we will use xampp server to connect a simple HTML web page to the database.
Download and install the xampp server on your computer. You can check the complete reference for xampp local server configuration.
How to download and Install Xampp local sever?
Step 2- In this step, you have to create a database on localhost/phpmyadmin/ PHP admin panel and create a table inside the database name.
I. Create a new database.
II. Create a table inside the database.
To connect the HTML page to the MYSQ database using PHP, you have to create a table. You can create an MYSQL table manually or use the query below.
Create a table using the query below-
CREATE TABLE `user` (
`id` INT( 40 ) NOT NULL ,
`uname` VARCHAR( 30 ) NOT NULL ,
`upassword` VARCHAR( 40 ) NOT NULL
) ENGINE = INNODB DEFAULT CHARSET = latin1;
Step 3- We will use an HTML form to connect the database using PHP. Here we will create a simple login form in PHP. To connect the HTML form to the database, we insert some data in the database table. You can use the signUp system to create a new user or insert it manually using the query.
INSERT INTO `user`(`id`, `uname`, `upassword`) VALUES (1,'demo','demo@123');
UserName=demo
Password = demo@123
Step 4- Now, create an HTML form. We will connect the HTML form to the database using a PHP script.
<form action="" method="POST">
<b>Username:-</b><input type="text" name="uname" required=""><br><br>
<b>Password:-</b><input type="text" name="upassword" required=""><br><br>
<input type="submit" name="sub" value="Login">
<form>
Step 5- Now create a connection file. A connection file writes in the PHP script. This connection file is used to connect the HTML page to the MYSQL database.
Create a connection file and save it as - config.php
<?php
$databaseHost = '127.0.0.1';//or localhost
$databaseName = 'test'; // your db_name
$databaseUsername = 'root'; // root by default for localhost
$databasePassword = ''; // by defualt empty for localhost
$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);
?>
Step 6- Now create PHP script to connect HTML pages .
In the PHP script, we use many pre-defined functions. These functions are known as built-in functions in PHP.
<?php
include("config.php");
if(isset($_POST['sub']))
{
$uname = $_POST['uname'];
$upassword = $_POST['upassword'];
$res = mysqli_query($mysqli,"select* from user where uname='$uname'and upassword='$upassword'");
$result=mysqli_fetch_array($res);
if($result)
{
echo "You are login Successfully ";
//header("location:my-account.php"); // create my-account.php page for redirection
}
else
{
echo "failed ";
}
}
?>
We can create an HTML form and PHP script on the same page or on different pages.
Now, you can log in. In this way, you can connect simple HTML web pages to the MYSQL database using PHP.
References -
Login system in PHP
Signup system in PHP
CRUD application in PHP