{"id":1106,"date":"2018-12-29T04:25:06","date_gmt":"2018-12-29T04:25:06","guid":{"rendered":"https:\/\/www.raghwendra.com\/blog\/?p=1106"},"modified":"2023-06-25T07:27:37","modified_gmt":"2023-06-25T07:27:37","slug":"how-to-connect-html-to-database-with-mysql-using-php-example","status":"publish","type":"post","link":"https:\/\/www.raghwendra.com\/blog\/how-to-connect-html-to-database-with-mysql-using-php-example\/","title":{"rendered":"How to connect HTML to database with MySQL using PHP? An example"},"content":{"rendered":"<p><strong><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-1439\" src=\"https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/how-to-connect-html-to-database-using-mysql-php-example.jpg\" alt=\"How to connect HTML to database with MySQL using PHP? An example\" width=\"1600\" height=\"830\" srcset=\"https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/how-to-connect-html-to-database-using-mysql-php-example.jpg 1600w, https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/how-to-connect-html-to-database-using-mysql-php-example-300x156.jpg 300w, https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/how-to-connect-html-to-database-using-mysql-php-example-1024x531.jpg 1024w, https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/how-to-connect-html-to-database-using-mysql-php-example-768x398.jpg 768w, https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/how-to-connect-html-to-database-using-mysql-php-example-1536x797.jpg 1536w\" sizes=\"(max-width: 1600px) 100vw, 1600px\" \/>How to connect HTML to database with MySQL using PHP? An example<\/strong> &#8211; This article helps to become a <a title=\"custom php developer\" href=\"https:\/\/www.raghwendra.com\/custom-php-web-application-development\"><strong><em>custom PHP developer<\/em><\/strong><\/a>. You will get complete steps for storing HTML form input field in MySQL database connection in a db table using the PHP programming with example . This article provide you HTML form, DB + Table SQL code, Bootstrap 5 with CSS, Form Validation and database connection +\u00a0 submission code . \u00a0In the conclusion step, you will be <span style=\"color: #339966;\"><strong>GIT download link<\/strong><\/span> so no need to copy-paste the code.<\/p>\n<h2>Tools Required to connect HTML Form with MySQL Database using PHP<\/h2>\n<p>First of all, you must be install any <strong>XAMPP or WAMP or MAMP (for Mac OS)<\/strong> kind of software on your laptop or computer. With this software, you will get a local webserver i.e. Apache, PHP language, and MySQL database. The complete code is on <em><strong>Github and the download link<\/strong><\/em> is the last of this article.<\/p>\n<p>In this article, my <strong>PHP, MySQL example<\/strong> is with <strong>database connection in xampp code<\/strong>.<\/p>\n<p>After installation you need to on the Xampp see the image below:<br \/>\n<img decoding=\"async\" class=\"aligncenter size-full wp-image-1450\" src=\"https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/xamps.jpg\" alt=\"Xampp Apache and MySQL on \" width=\"713\" height=\"669\" srcset=\"https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/xamps.jpg 713w, https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/xamps-300x281.jpg 300w\" sizes=\"(max-width: 713px) 100vw, 713px\" \/><\/p>\n<p>After installation of any 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.1 or http:\/\/localhost\/ . If this is working it means you have the local webserver activated with PHP\/MySQL.<\/p>\n<p>Also, GUI PHPmyAdmin coming for handling <a href=\"https:\/\/www.raghwendra.com\/blog\/insert-update-delete-php-mysql-example-git-downloads\/\">CRUD operations i.e. insert(create), update, delete, and select(read) records<\/a> from tables. This interface is browser-based and very helpful, easy to use for creating and managing phpmyadmin database in table(column, row).<\/p>\n<p>If you have the above installation you can go ahead to start your coding.<\/p>\n<p>If you have not a LAMP stack-based web server then you can do this directly in your hosting space.<\/p>\n<p>If you have any more query then you can comment on this post. We will reply to your query.<\/p>\n<a rel=\"noreferrer\" href=\"https:\/\/www.youtube.com\/watch?v=iL-GbPEOyzw\" class=\"vp-a vp-yt-type vp-modal-click vp-modal-click_shortcode\">Watch Video<\/a>\n<p>Suppose you have a web page to insert contact form field data in your DB. For this you need to follow the following steps:<\/p>\n<h2>Step 1: Filter your HTML form requirements for your contact us web page<\/h2>\n<p>Suppose you selected the form field Name (text input), Email(email input), Phone (number input), and message (multi-line text). The form submit button also necessary for submitting the form. You will get the complete form in HTML coding in step 3.<\/p>\n<h2>Step 2: Create a database and a table in MySQL<\/h2>\n<p>Open a 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 computer. See the xampp screen below how it is coming.<\/p>\n<p>Click on the databases link and create your db by the name \u201cdb_contact\u201d. See the image below:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-1130\" src=\"https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/db_connect.jpg\" alt=\"\" width=\"987\" height=\"539\" srcset=\"https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/db_connect.jpg 987w, https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/db_connect-300x164.jpg 300w, https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/db_connect-768x419.jpg 768w\" sizes=\"(max-width: 987px) 100vw, 987px\" \/><\/p>\n<p>After creating your DB you need to create a table by any name I choose \u201ctbl_contact\u201d with the number of field 5. We choose 4 fields on top Name, Email, Phone, and Message. The first column we will keep for maintaining the serial number and in technical terms primary key(unique number of each recor). See the image below<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1134\" src=\"https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/table.jpg\" alt=\"\" width=\"987\" height=\"282\" srcset=\"https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/table.jpg 987w, https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/table-300x86.jpg 300w, https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/table-768x219.jpg 768w\" sizes=\"(max-width: 987px) 100vw, 987px\" \/><\/p>\n<p>When you will click to go button you will get this screen. Now we need to feed every field information.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1131\" src=\"https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/field_blank.jpg\" alt=\"\" width=\"987\" height=\"493\" srcset=\"https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/field_blank.jpg 987w, https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/field_blank-300x150.jpg 300w, https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/field_blank-768x384.jpg 768w\" sizes=\"(max-width: 987px) 100vw, 987px\" \/><\/p>\n<p>See the below image in which I added field information. So for field Name used field Name &#8211; fldName, Email &#8211; fldEmail, Phone &#8211; fldPhone, Message &#8211; fldMessage.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1132\" src=\"https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/field_field.jpg\" alt=\"\" width=\"987\" height=\"493\" srcset=\"https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/field_field.jpg 987w, https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/field_field-300x150.jpg 300w, https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/field_field-768x384.jpg 768w\" sizes=\"(max-width: 987px) 100vw, 987px\" \/><\/p>\n<p>Now click on the save button that is on the bottom right of your screen. After saving your table it is created in your database.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1129\" src=\"https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/browse.jpg\" alt=\"\" width=\"987\" height=\"493\" srcset=\"https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/browse.jpg 987w, https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/browse-300x150.jpg 300w, https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/browse-768x384.jpg 768w\" sizes=\"(max-width: 987px) 100vw, 987px\" \/><br \/>\nYou can create your DB and table using the SQL below. You have to copy the following code and paste it into your MySQL GUI\u00a0 phpmyadmin database or any other GUI or command prompt. At the bottom of the blog, you will get a <strong>git download link<\/strong> to download the SQL file.<\/p>\n<pre><code>\r\n\r\n--\r\n-- Database: `mydb`\r\n--\r\n\r\nCREATE DATABASE IF NOT EXISTS `db_contact` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;\r\nUSE `db_contact`;\r\n\r\n-- --------------------------------------------------------\r\n\r\n--\r\n-- Table structure for table `tbl_contact`\r\n--\r\n\r\nDROP TABLE IF EXISTS `tbl_contact`;\r\nCREATE TABLE IF NOT EXISTS `tbl_contact` (\r\n`id` int(11) NOT NULL,\r\n  `fldName` varchar(50) NOT NULL,\r\n  `fldEmail` varchar(150) NOT NULL,\r\n`fldPhone` varchar(15) NOT NULL,\r\n`fldMessage` text NOT NULL\r\n) ENGINE=InnoDB DEFAULT CHARSET=latin1;\r\n\r\n--\r\n-- Indexes for dumped tables\r\n--\r\n\r\n--\r\n-- Indexes for table `tbl_contact`\r\n--\r\nALTER TABLE `tbl_contact`\r\nADD PRIMARY KEY (`id`);\r\n\r\n--\r\n-- AUTO_INCREMENT for dumped tables\r\n--\r\n\r\n--\r\n-- AUTO_INCREMENT for table `tbl_contact`\r\n--\r\nALTER TABLE `tbl_contact`\r\nMODIFY `id` int(11) NOT NULL AUTO_INCREMENT;\r\n<\/code><\/pre>\n<h2>Step 3: Create HTML form for connecting to database<\/h2>\n<p>Now you have to create an HTML form. For this, you need to create a working folder first and then create a web page with the name \u201ccontact.html\u201d. If you install xampp your working folder is in folder this \u201cE:\\xampp\\htdocs\u201d. You can create a new folder \u201ccontact\u201d on your localhost working folder. Create a \u201ccontact.html\u201d file and paste the following code.<\/p>\n<pre><code>&lt;!DOCTYPE html&gt;\r\n&lt;html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"&gt;\r\n&lt;head&gt;\r\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/&gt;\r\n&lt;title&gt;Contact Form - PHP\/MySQL Demo Code&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n&lt;fieldset&gt;\r\n&lt;legend&gt;Contact Form&lt;\/legend&gt;\r\n&lt;form name=\"frmContact\" method=\"post\" action=\"contact.php\"&gt;\r\n&lt;p&gt;\r\n&lt;label for=\"Name\"&gt;Name &lt;\/label&gt;\r\n&lt;input type=\"text\" name=\"txtName\" id=\"txtName\"&gt;\r\n&lt;\/p&gt;\r\n&lt;p&gt;\r\n&lt;label for=\"email\"&gt;Email&lt;\/label&gt;\r\n&lt;input type=\"text\" name=\"txtEmail\" id=\"txtEmail\"&gt;\r\n&lt;\/p&gt;\r\n&lt;p&gt;\r\n&lt;label for=\"phone\"&gt;Phone&lt;\/label&gt;\r\n&lt;input type=\"text\" name=\"txtPhone\" id=\"txtPhone\"&gt;\r\n&lt;\/p&gt;\r\n&lt;p&gt;\r\n&lt;label for=\"message\"&gt;Message&lt;\/label&gt;\r\n&lt;textarea name=\"txtMessage\" id=\"txtMessage\"&gt;&lt;\/textarea&gt;\r\n&lt;\/p&gt;\r\n&lt;p&gt;&amp;nbsp;&lt;\/p&gt;\r\n&lt;p&gt;\r\n&lt;input type=\"submit\" name=\"Submit\" id=\"Submit\" value=\"Submit\"&gt;\r\n&lt;\/p&gt;\r\n&lt;\/form&gt;\r\n&lt;\/fieldset&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/code><\/pre>\n<p>Now your form is ready. You may test it in your localhost link http:\/\/localhost\/contact\/contact.html<br \/>\nIn the next step, I will go with creating PHP \/ MySQL code.<\/p>\n<h2>Step 4: Create a PHP page to save data from HTML form to your MySQL database<\/h2>\n<p>The contact HTML form action is on \u201ccontact.php\u201d page. On this page, we will write code for inserting records into the database.<\/p>\n<p>For storing data in MySQL as records, you 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 <code>mysqli_connect.<\/code><\/p>\n<pre><code>$con = mysqli_connect(\"localhost\",\"your_localhost_database_user\",\"your_localhost_database_password\",\"your_localhost_database_db\");\r\n<\/code><\/pre>\n<p>You need to place value for your localhost username and password. Normally localhost MySQL database username is root and password blank or root. For example, the code is as below<\/p>\n<pre><code>$con = mysqli_connect('localhost', 'root', '',\u2019db_contact\u2019);\r\nThe \u201cdb_contact\u201d is our database name that we created before.\r\nAfter connection database you need to take post variable from the form. See the below code\r\n$txtName = $_POST['txtName'];\r\n$txtEmail = $_POST['txtEmail'];\r\n$txtPhone = $_POST['txtPhone'];\r\n$txtMessage = $_POST['txtMessage'];<\/code><\/pre>\n<p>When you will get the post variable then you need to write the following SQL command.<\/p>\n<pre><code>$sql = \"INSERT INTO `tbl_contact` (`Id`, `fldName`, `fldEmail`, `fldPhone`, `fldMessage`) VALUES ('0', '$txtName', '$txtEmail', '$txtPhone', '$txtMessage');\"<\/code><\/pre>\n<p>For fire query over the database, you need to write the following line<\/p>\n<pre><code>$rs = mysqli_query($con, $sql);\r\n<\/code><\/pre>\n<p>Here is PHP code for inserting data into your database from a form.<\/p>\n<pre><code>&lt;?php\r\n\/\/ database connection code\r\n\/\/ $con = mysqli_connect('localhost', 'database_user', 'database_password','database');\r\n\r\n$con = mysqli_connect('localhost', 'root', '','db_contact');\r\n\r\n\/\/ get the post records\r\n$txtName = $_POST['txtName'];\r\n$txtEmail = $_POST['txtEmail'];\r\n$txtPhone = $_POST['txtPhone'];\r\n$txtMessage = $_POST['txtMessage'];\r\n\r\n\/\/ database insert SQL code\r\n$sql = \"INSERT INTO `tbl_contact` (`Id`, `fldName`, `fldEmail`, `fldPhone`, `fldMessage`) VALUES ('0', '$txtName', '$txtEmail', '$txtPhone', '$txtMessage')\";\r\n\r\n\/\/ insert in database \r\n$rs = mysqli_query($con, $sql);\r\n\r\nif($rs)\r\n{\r\n\techo \"Contact Records Inserted\";\r\n}\r\n\r\n?&gt;<\/code><\/pre>\n<h2>Step 5:\u00a0 All done!<\/h2>\n<p>Now the coding part is done.\u00a0 <a href=\"https:\/\/github.com\/wdraghwendra\/phpmysql\/tree\/master\/contact\" target=\"_blank\" rel=\"noopener\">Download code from github<\/a><\/p>\n<p>If you would like to check then you can fill the form http:\/\/localhost\/contact\/contact.html and see the result in the database. You may check via phpmyadmin your inserted record.<\/p>\n<h3>Why skills as a <strong>custom PHP developer<\/strong>?<\/h3>\n<p>Php is the most popular server-side programming language. It is used more than 70% in comparison to other website development languages. As a lot of CMS and custom PHP applications developed already on PHP, therefore, it will be a demanding language for the next 5 years.<\/p>\n<p>The worldwide <a title=\"PHP development company\" href=\"https:\/\/www.raghwendra.com\/\" target=\"_blank\" rel=\"noopener\">PHP development company<\/a> is looking for cheap <em>PHP developers in India<\/em>. Many companies also like to freelance PHP developers in Delhi, London, Bangalore, Mumbai (locally).\u00a0 If you would like to <strong>hire a dedicated developer<\/strong> then you need to skills yourself.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1129\" src=\"https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/browse.jpg\" alt=\"\" width=\"987\" height=\"493\" srcset=\"https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/browse.jpg 987w, https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/browse-300x150.jpg 300w, https:\/\/www.raghwendra.com\/blog\/wp-content\/uploads\/2018\/12\/browse-768x384.jpg 768w\" sizes=\"(max-width: 987px) 100vw, 987px\" \/><\/p>\n<h5>See more answer about PHP script connect to Mysql on Facebook Group<\/h5>\n<p>Please join Facebook group for discussion <a title=\"Facebook Group click here\" href=\"https:\/\/www.facebook.com\/groups\/226548845512051\/\" target=\"_blank\" rel=\"nofollow noopener\">click here<\/a><br \/>\nPost your question here with the HASH tag\u00a0 #connectphpmysql\u00a0 #connecthtmlmysql\u00a0 . We will approve and answer your question.<\/p>\n<p>Please view more answer on this hashtag on Facebook Group <a href=\"https:\/\/www.facebook.com\/hashtag\/connectphpmysql?__gid__=226548845512051\" target=\"_blank\" rel=\"nofollow noopener\">#connectphpmysql<\/a>\u00a0 <a title=\"connecthtmlmysql\" href=\"https:\/\/www.facebook.com\/hashtag\/connecthtmlmysql?__gid__=226548845512051\" target=\"_blank\" rel=\"nofollow noopener\">#connecthtmlmysql<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to connect HTML to database with MySQL using PHP? An example &#8211; This article helps to become a custom PHP developer.<\/p>\n","protected":false},"author":7,"featured_media":1137,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[240,243,242,241,239],"class_list":["post-1106","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-open-source-php","tag-connect-html-to-database","tag-connecthtmlmysql","tag-connectphpmysql","tag-contact-us-form-php","tag-php-web-developer","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.raghwendra.com\/blog\/wp-json\/wp\/v2\/posts\/1106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.raghwendra.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.raghwendra.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.raghwendra.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.raghwendra.com\/blog\/wp-json\/wp\/v2\/comments?post=1106"}],"version-history":[{"count":0,"href":"https:\/\/www.raghwendra.com\/blog\/wp-json\/wp\/v2\/posts\/1106\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.raghwendra.com\/blog\/wp-json\/wp\/v2\/media\/1137"}],"wp:attachment":[{"href":"https:\/\/www.raghwendra.com\/blog\/wp-json\/wp\/v2\/media?parent=1106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.raghwendra.com\/blog\/wp-json\/wp\/v2\/categories?post=1106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.raghwendra.com\/blog\/wp-json\/wp\/v2\/tags?post=1106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}