×

Please Login or Register to continue.

4
(2.4K Views)

Hey Community! 

I am a 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 the 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. 

(2.2K Points)
in PHP

Share

2 Answers
(9.2K Points)
1

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. 

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 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 


Comment

(200 Points)
2

First of all you need to install xamp, wamp, or mamp any software required to be installed in your system with the help of which you will get a local webserver apache. Once it is installed, you need to check whether localhost is working properly or not. The first step is to filter out your HTML form requirements, then need to create the database. In this section, open the web browser and type http://localhost/phpmyadmin for open GUI for managing the database on your computer. Click on the database link and create the database by name and then create a table with any number. Now create an HTML form and once the form is created, then the next step is to create PHP or MYSQL code.

Now suppose you have created database db_contact, table tbl_contact, create a web page with the name of contact.htm, then in the last, we will create PHP page. contact.php page includes contacting HTML form action where you will write code for inserting a record in the database. For connecting database, the following code is very simple.

 

$con= mysqli_connect("localhost", "your_localhost_database_user",     "your_localhost_database_password", "your_localhost_database_db");



Normally the localhost mysql database username is root and the password is either blank or root.

For Xamp, the code is as below:
 

$con= mysqli_connect('localhost', 'root', '', 'db_connect');



where db_connect is the database name. After the connection of the database, you need to take the post variable from the form. 

$txtname= $_POST['txtname'];

$txtEmail= $_POST['txtemail'];

$txtPhone= $_POST['txtphone'];

$txtMessage= $_POST['txtmessage'];

 


Comment

Related Questions :-

Your Answer

You can post your answer after login..