How to connect HTML to database with MySQL using PHP? An exampleHow to connect HTML to database with MySQL using PHP? An example – This article helps to become a custom PHP programmer . You lot will get complete steps for PHP database connection instance program. This article provide you lot HTML form, DB + Table SQL code, Boostrap v with CSS, Grade Validation and database connection +  submission code .  In the conclusion step, you volition be GIT download link so no demand to re-create-paste the code.

Tools Required to connect HTML Form with MySQL Database using PHP

First of all, you lot must exist install any XAMPP or WAMP or MAMP kind of software on your laptop or computer. With this software, you will go a local webserver i.e. Apache, PHP language, and MySQL database. The complete code is on Github and the download link is the last of this article.

In this article, my PHP, MySQL example is with database connectedness in xampp code.

After installation yous need to on the Xampp see the paradigm below:
Xampp Apache and MySQL on

Afterward installation of whatsoever of these laptop or desktop software you need to check your localhost is working or not. Open your browser and check this URL http://127.0.0.ane or http://localhost/ . If this is working it means you lot have the local webserver activated with PHP/MySQL.

Also, GUI PHPmyAdmin coming for handling CRUD operations i.due east. insert(create), update, delete, and select(read) records from tables. This interface is browser-based and very helpful, easy to use for creating and managing phpmyadmin database in table(column, row).

If you accept the above installation you can get ahead to start your coding.

If you have not a LAMP stack-based web server and so you can do this directly in your hosting space.

If you have any more query then you can comment on this post. We will reply to your query.

Suppose y'all have a web page to insert contact form field data in your DB. For this you demand to follow the post-obit steps:

Step ane: Filter your HTML form requirements for your contact the states web folio

Suppose you selected the course field Name (text input), Electronic mail(email input), Phone (number input), and bulletin (multi-line text). The grade submit button also necessary for submitting the form. You will get the complete course in HTML coding in step iii.

Pace 2: Create a database and a tabular array in MySQL

Open a spider web browser (chrome, firefox, edge, etc., ) and type this http://localhost/phpmyadmin/ or http://127.0.0.1/phpmyadmin/ for open GUI for managing DB on your reckoner. Run across the xampp screen below how information technology is coming.

Click on the databases link and create your db past the name "db_contact". Run across the image below:

After creating your DB you need to create a table by whatsoever name I cull "tbl_contact" with the number of field 5. We choose 4 fields on peak Proper name, Email, Phone, and Message. The beginning cavalcade we volition proceed for maintaining the serial number and in technical terms chief cardinal(unique number of each recor). See the image below

When yous will click to go button you lot will become this screen. At present nosotros need to feed every field information.

See the below image in which I added field data. Then for field Proper noun used field Name – fldName, Email – fldEmail, Phone – fldPhone, Message – fldMessage.

At present click on the save push that is on the bottom right of your screen. After saving your table it is created in your database.


Y'all can create your DB and table using the SQL below. Yous have to copy the following code and paste it into your MySQL GUI  phpmyadmin database or any other GUI or command prompt. At the bottom of the blog, yous will get a git download link to download the SQL file.

                              -- -- Database: `mydb` --  CREATE DATABASE IF NOT EXISTS `db_contact` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci; Apply `db_contact`;  -- --------------------------------------------------------  -- -- Table structure for table `tbl_contact` --  Driblet Tabular array IF EXISTS `tbl_contact`; CREATE Tabular array IF Not EXISTS `tbl_contact` ( `id` int(11) Not NULL, `fldName` int(fifty) Non Cypher, `fldEmail` int(150) Non Zilch, `fldPhone` varchar(15) NOT NULL, `fldMessage` text Not Nada ) ENGINE=InnoDB DEFAULT CHARSET=latin1;  -- -- Indexes for dumped tables --  -- -- Indexes for table `tbl_contact` -- Alter TABLE `tbl_contact` ADD PRIMARY Cardinal (`id`);  -- -- AUTO_INCREMENT for dumped tables --  -- -- AUTO_INCREMENT for table `tbl_contact` -- Alter Table `tbl_contact` Modify `id` int(11) Not Goose egg AUTO_INCREMENT;                          

Stride 3: Create HTML form for connecting to database

Now yous have to create an HTML grade. For this, you need to create a working folder commencement and then create a spider web page with the proper name "contact.html". If you install xampp your working binder is in folder this "E:\xampp\htdocs". Yous tin can create a new folder "contact" on your localhost working binder. Create a "contact.html" file and paste the following lawmaking.

              <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <caput> <meta http-equiv="Content-Type" content="text/html; charset=utf-viii" /> <championship>Contact Form - PHP/MySQL Demo Code</championship> </head>  <body> <fieldset> <legend>Contact Form</legend> <form name="frmContact" method="post" action="contact.php"> <p> <label for="Name">Name </label> <input blazon="text" name="txtName" id="txtName"> </p> <p> <label for="email">Email</characterization> <input type="text" name="txtEmail" id="txtEmail"> </p> <p> <label for="phone">Phone</label> <input type="text" name="txtPhone" id="txtPhone"> </p> <p> <label for="bulletin">Message</label> <textarea name="txtMessage" id="txtMessage"></textarea> </p> <p>&nbsp;</p> <p> <input type="submit" name="Submit" id="Submit" value="Submit"> </p> </form> </fieldset> </body> </html>                          

At present your class is ready. You lot may test it in your localhost link http://localhost/contact/contact.html
In the next step, I will go with creating PHP / MySQL lawmaking.

Step 4: Create a PHP page to save data from HTML course to your MySQL database

The contact HTML grade action is on "contact.php" page. On this page, we will write code for inserting records into the database.

For storing data in MySQL as records, yous have to first connect with the DB. Connecting the code is very simple. The mysql_connect in PHP is deprecated for the latest version therefore I used it here mysqli_connect.

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

You need to place value for your localhost username and countersign. Unremarkably localhost MySQL database username is root and password blank or root. For instance, the code is every bit below

              $con = mysqli_connect('localhost', 'root', '','db_contact'); The "db_contact" is our database name that we created before. Subsequently connection database you demand to take post variable from the form. See the below code $txtName = $_POST['txtName']; $txtEmail = $_POST['txtEmail']; $txtPhone = $_POST['txtPhone']; $txtMessage = $_POST['txtMessage'];            

When you lot will get the postal service variable and so you demand to write the following SQL control.

              $sql = "INSERT INTO `tbl_contact` (`Id`, `fldName`, `fldEmail`, `fldPhone`, `fldMessage`) VALUES ('0', '$txtName', '$txtEmail', '$txtPhone', '$txtMessage');"            

For fire query over the database, you lot need to write the following line

              $rs = mysqli_query($con, $sql);                          

Here is PHP lawmaking for inserting data into your database from a form.

              <?php // database connection lawmaking // $con = mysqli_connect('localhost', 'database_user', 'database_password','database');  $con = mysqli_connect('localhost', 'root', '','db_contact');  // get the post records $txtName = $_POST['txtName']; $txtEmail = $_POST['txtEmail']; $txtPhone = $_POST['txtPhone']; $txtMessage = $_POST['txtMessage'];  // database insert SQL lawmaking $sql = "INSERT INTO `tbl_contact` (`Id`, `fldName`, `fldEmail`, `fldPhone`, `fldMessage`) VALUES ('0', '$txtName', '$txtEmail', '$txtPhone', '$txtMessage')";  // insert in database  $rs = mysqli_query($con, $sql);  if($rs) { 	echo "Contact Records Inserted"; }  ?>            

Footstep v:  All done!

Now the coding role is done.  Download lawmaking from github

If you would like to check and then you lot can make full the form http://localhost/contact/contact.html and see the result in the database. You may check via phpmyadmin your inserted record.

Why skills as a custom PHP programmer?

Php is the nigh popular server-side programming language. It is used more than than lxx% in comparison to other website evolution languages. As a lot of CMS and custom PHP applications developed already on PHP, therefore, information technology will be a demanding language for the next v years.

The worldwide PHP development company is looking for inexpensive PHP developers in India. Many companies also like to freelance PHP developers in Delhi, London, Bangalore, Mumbai (locally).  If you would like to hire a dedicated developer and then you need to skills yourself.

Come across more than respond about PHP script connect to Mysql on Facebook Group

Please join Facebook group for discussion click here
Mail service your question hither with the HASH tag  #connectphpmysql  #connecthtmlmysql  . We will approve and respond your question.

Please view more respond on this hashtag on Facebook Group #connectphpmysql  #connecthtmlmysql