Welcome to Techno Smarter QA, Questions, Answers Community
+3 votes
98 views

Hey Community! 

I am the learner of PHP programming. I have completed HTML language and now I am trying to learn the PHP language. I want to connect the HTML page to PHP with the MYSQL database.  I tried many times to connect a simple HTML page to PHP but did not get any results. I can create HTML web pages but I don't know how can connect HTML page to PHP. I know about the MYSQL database but I don't know connection between HTML and PHP. I am a fast learner . Please give me the best solution to connect the HTML page with PHP using the MYSQL database. 

in PHP by (2k points) 2 10 40

1 Answer

+1 vote
 
Best answer

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 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 to understand 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 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. 

xampp-wamp-local server global server

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 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 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 MYSQL database. 

Create a connection file and save 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 HTML form and PHP script in the same page or 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 

by (8.9k points) 9 22 61
selected by
...