How To Connect HTML Form and MySQL Database Using Xampp Server phpMyAdmin Mysql Tutorial
How To Connect HTML Form and MySQL Database Using Xampp Server phpMyAdmin Mysql Tutorial
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 “contact.html”. If you install xampp your
working folder is in folder this “E:\xampp\htdocs”. You can create a new folder “contact”
on your localhost working folder. Create a “contact.html” file and paste the following
code.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</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.
$con = mysqli_connect("localhost","your_localhost_database_user","your_localhost_data
base_password","your_localhost_database_db");
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’);
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`, `fldMessa
ge`) 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)
?>