How To Connect HTML To Database With MySQL Using PHP
How To Connect HTML To Database With MySQL Using PHP
An
example – This article helps to become a custom PHP developer.
You will get complete steps for PHP database connection example
program. This article provide you HTML form, DB + Table SQL code,
Boostrap 5 with CSS, Form Validation and database connection +
submission code .
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.
You can create your DB and table using the SQL below. You 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, you 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;
USE `db_contact`;
-- --------------------------------------------------------
--
--
--
--
--
--
--
--
--
--
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<fieldset>
<legend>Contact Form</legend>
<p>
</p>
<p>
<label for="email">Email</label>
</p>
<p>
<label for="phone">Phone</label>
</p>
<p>
<label for="message">Message</label>
<p> </p>
<p>
</p>
</form>
</fieldset>
</body>
</html>
Now your form is ready. You may test it in your localhost link
http://localhost/contact/contact.html
In the next step, I will go with creating PHP / MySQL code.
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
$con = mysqli_connect('localhost', 'root', '',’db_contact’);
The “db_contact” is our database name that we created before.
After connection database you need 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 will get the post variable then you need to write the
following SQL command.
$sql = "INSERT INTO `tbl_contact` (`Id`, `fldName`, `fldEmail`, `fldPhone`,
`fldMessage`) VALUES ('0', '$txtName', '$txtEmail', '$txtPhone', '$txtMessage');"
For fire query over the database, you need to write the following line
$rs = mysqli_query($con, $sql);
Here is PHP code for inserting data into your database from a form.
<?php
$txtName = $_POST['txtName'];
$txtEmail = $_POST['txtEmail'];
$txtPhone = $_POST['txtPhone'];
$txtMessage = $_POST['txtMessage'];
// insert in database
if($rs)
?>