0% found this document useful (0 votes)
66 views3 pages

How To Connect HTML Form and MySQL Database Using Xampp Server phpMyAdmin Mysql Tutorial

Connecting html with msql

Uploaded by

Tesfaye Kifle
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
66 views3 pages

How To Connect HTML Form and MySQL Database Using Xampp Server phpMyAdmin Mysql Tutorial

Connecting html with msql

Uploaded by

Tesfaye Kifle
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

Step 3: Create HTML form for connecting to database

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>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Contact Form - PHP/MySQL Demo Code</title>

</head>

<body>

<fieldset>

<legend>Contact Form</legend>

<form name="frmContact" method="post" action="contact.php">

<p>

<label for="Name">Name </label>

<input type="text" name="txtName" id="txtName">

</p>

<p>

<label for="email">Email</label>

<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="message">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>

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.

Step 4: Create a PHP page to save data from HTML form


to your MySQL database
The contact HTML form 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, 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 mysqli_connect.

$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’);

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

// database connection code

// $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 code

$sql = "INSERT INTO `tbl_contact` (`Id`, `fldName`, `fldEmail`, `fldPhone`, `fldMessa


ge`) VALUES ('0', '$txtName', '$txtEmail', '$txtPhone', '$txtMessage')";

// insert in database

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

if($rs)

echo "Contact Records Inserted";

?>

You might also like