How to Create Contact Form using PHP MySQL

0 49,653

Contact Form using PHP MySQl. PHP is a great scripting luanguage that allows many dynamic functions in to websites. you can create customs forms, forms validation using php. in this article we will discuss about How to create contact form using PHP MySQL. A contact form allows visitor to communicate with website owner. By using this form you can easily connect it to your database and keep a record of users whoe are trying to contact you.

How to Create Contact Form using PHP MySQL

1-Creating Database

  • Open Phpmyadmin in your Browser
  • Click on Database Tab Display on Top side
  • Give the Database name “contact_form”.
  • After Creating Database Open it.
  • Click on SQL Tab on Top area
  • Copy the Below Source Code and paste it.
  • Then Click on Go.
-- phpMyAdmin SQL Dump
-- version 4.2.7.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Apr 30, 2016 at 01:27 PM
-- Server version: 5.6.20
-- PHP Version: 5.5.15

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `contact_form`
--

-- --------------------------------------------------------

--
-- Table structure for table `tbl_contact`
--

CREATE TABLE IF NOT EXISTS `tbl_contact` (
`tbl_contact_id` int(11) NOT NULL,
  `fullname` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL,
  `user_message` varchar(100) NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

--
-- Dumping data for table `tbl_contact`
--

INSERT INTO `tbl_contact` (`tbl_contact_id`, `fullname`, `email`, `user_message`) VALUES
(5, 'Priyanshu Raj', 'example@email.com', 'Hi . I have a question? '),
(6, 'Raj', 'example@email.com', 'This is testing message..'),
(7, 'Mohit...', 'example@email.com', 'Hello. I am mohit I want to create a website.. please tell me how to create it.');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `tbl_contact`
--
ALTER TABLE `tbl_contact`
 ADD PRIMARY KEY (`tbl_contact_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tbl_contact`
--
ALTER TABLE `tbl_contact`
MODIFY `tbl_contact_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=8;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

OR Import DB File

After Downloading the source code extract it in your root folder.

  • Open Phpmyadmin in your Browser
  • Click on Database Tab Display on Top side
  • Give the Database name “contact_form”.
  • After Creating Database Open it.
  • Click on Import Tab on Top area
  • You can Find Db file in  Downloaded source code Select it.
  • Then Click on Go.

2- Creating Database Connection

After import Database File then next step is creating database connection using php copy the below code.

$conn = mysql_connect("localhost","root","");
mysql_select_db("contact_form",$conn);

3 – Creating Contact Form using HTML

Next step is creating contact form and in this step we are going to create contact form using html. create a  contact form giving below with simple html validation. value which will be written between the double qoutes in attributes name like  name=”fullname” in input tags work as varriable name. these attributes will contain the data from the form that we will use to save in our database.

<form name="frmContact" method="post" action="">
<div class="aler_message"> <?php if(isset($message)) { echo $message; } ?> </div>
<table border="0" cellpadding="10" cellspacing="1" width="500" align="center">
<tr class="tableheader">
<td colspan="2">Contact Form</td>
</tr>
<tr class="tablerow">
<td>Full Name <br />
<input type="text" class="text_input" autofocus="autofocus" name="fullname">
</td>
<td>Email <br />
<input type="text" class="text_input" autofocus="autofocus" name="email">
</td>
</tr>
<tr class="tablerow">
<td colspan="2">Message <br />
<textarea name="user_message" class="text_input" autofocus="autofocus" cols="60" rows="6"></textarea>
</td>
</tr>
<tr class="tableheader">
<td colspan="2">
<input type="submit" class="btn_submit" name="submit" value="Submit">
</td>
</tr>
</table>
</form>

4 – Styling the Contact form using CSS

Next step is give the style to contact form for making attractive.

table {
	border:red 5px solid;
}
.table_data {
    margin-left: 291px;
    margin-top: 20px;
} 
.tableheader {
	background-color: tan;
    color: white;
    font-weight: bold;
    font-size: 25px;
}
.tablerow {
	background-color: tan;
    color: white;
    font-size: 20px;
    font-weight: bold;
}
.aler_message {
	color: #FF0000;
	font-weight: bold;
	text-align: center;
	width: 100%;
}
.text_input {
	font-size: 18px;
    border: blue 1px solid;
    background: azure;
    text-indent: 10px;
}
.btn_submit {
    font-size: 18px;
    width: 100px;
    background: azure;
    border: blue 1px solid;
    padding: 6px;
    border-radius: 8px;
    color: blue;
}
.btn_submit:hover {
    font-size: 18px;
    width: 100px;
    background: black;
    border: red 1px solid;
    padding: 6px;
    border-radius: 8px;
    color: white;
	cursor:pointer;
}

5 – Insert Data into database using PHP

in this step we will insert form data into database using php.

<?php
if(!empty($_POST["submit"])) {
	$fullname = $_POST["fullname"];
	$email = $_POST["email"];
	$user_message = $_POST["user_message"];

	$conn = mysql_connect("localhost","root","");
	mysql_select_db("contact_form",$conn);
	mysql_query("INSERT INTO tbl_contact (fullname, email, user_message) VALUES ('" . $fullname. "', '" . $email. "','" . $user_message. "')");
	$insert_id = mysql_insert_id();
	if(!empty($insert_id)) {
	$message = "Successfully Added.";
	}
}
?>

6 – Display Inserted Data on Page

in this step we fetch data on Page from mysql Database using PHP.

<table style="border:2px red solid" class="table_data"  cellpadding="5">
	<tr>
		<th>
			Full Name
		</th>
		<th>
			Email
		</th>
		<th>
			Message
		</th>
	</tr>
<?php
$conn = mysql_connect("localhost","root","");
mysql_select_db("contact_form",$conn);
$result= mysql_query("select * from tbl_contact order by tbl_contact_id DESC ") or die (mysql_error());
while ($row= mysql_fetch_array ($result) ){
$id=$row['tbl_contact_id'];
?>
	<tr style="text-align:center;">
		<td style="width:200px;">
			<?php echo $row['fullname']; ?>
		</td>
		<td style="width:200px;">
			<?php echo $row['email']; ?>
		</td>
		<td style="width:200px; color:blue;">
			<?php echo $row['user_message']; ?>
		</td>
	</tr>
<?php } ?>
</table>

If you facing any type of problem with this source code then you can Download the Complete source code in zip Formate by clicking the below button Download Now otherwise you can send Comment.

Download Source Code

 

Leave A Reply

Your email address will not be published.