Create a Contact Form with PHP and MySQL β Practical Guide
Create a Contact Form with PHP and MySQL β Practical Guide. Get practical lessons and hands-on examples at AIComputerClasses in Indore to master programming & IT development skills quickly. Ideal for beginners and working professionals seeking fast skill gains.
2025-10-28 14:23:36 - AiComputerClasses
Creating a contact form using PHP and MySQL is one of the most practical beginner projects for anyone learning web development in 2025. Whether youβre a student at AI Computer Classes, Indore or an aspiring web developer, mastering this concept helps you understand how websites collect and process user input β a skill that forms the backbone of all dynamic web applications.
In todayβs digital-first world, almost every business website includes a βContact Usβ section. It allows users to share feedback, ask questions, or request services β and behind that simple interface lies the powerful combination of HTML, PHP, and MySQL. In this blog, weβll walk you through how to design, code, and connect a working contact form that stores messages securely in a database.
Before diving into the code, itβs essential to understand why we create contact forms:
- They allow visitors to communicate directly with website owners.
- They collect valuable user data (like name, email, and message).
- They prevent spam compared to exposing an email address.
- They can be customized for feedback, support, or business inquiries.
At AI Computer Classes β Indore, we teach students how to connect frontend design with backend logic β an essential step toward becoming a full-stack web developer.
π‘ Learn from Experts at AI Computer Classes β Indore!
Boost your career with hands-on courses in Web Development, PHP, and Database Management.
π Join our latest batch now at AI Computer Classes
π Located in Old Palasia, Indore
π§± Step 2: Setting Up the Project FolderTo start building, youβll need a simple folder structure:
contact-form/ β βββ index.html βββ style.css βββ process.php βββ database.sql
Tools Required:
- XAMPP or WAMP Server (for running PHP locally)
- Code Editor like VS Code or Sublime Text
- Web Browser (Chrome, Edge, or Firefox)
Once your environment is ready, open the project folder inside your editor.
Letβs create a simple, clean contact form in index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact Form</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="contact-container">
<h2>Contact Us</h2>
<form action="process.php" method="POST">
<input type="text" name="name" placeholder="Your Name" required>
<input type="email" name="email" placeholder="Your Email" required>
<textarea name="message" placeholder="Your Message" required></textarea>
<button type="submit" name="submit">Send Message</button>
</form>
</div>
</body>
</html>
This form uses the POST method to send data to process.php.
π Styling the Form (style.css)
body {
font-family: Arial, sans-serif;
background: #f3f3f3;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.contact-container {
background: #fff;
padding: 30px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
width: 400px;
}
.contact-container h2 {
text-align: center;
margin-bottom: 20px;
}
input, textarea {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
button {
width: 100%;
padding: 10px;
background: #0078D7;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
Now your form is ready with a beautiful, minimal look.
To store user submissions, weβll create a database table.
SQL Script (database.sql):
CREATE DATABASE contact_db; USE contact_db; CREATE TABLE messages ( id INT(11) AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(100) NOT NULL, message TEXT NOT NULL, submitted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
Import this SQL file into phpMyAdmin or run it using your local MySQL console.
π‘ Learn from Experts at AI Computer Classes β Indore!
Join our Web Development and PHP Mastery Program today!
π Enroll Now
π Practical learning with real-world projects in Indore.
π§ Step 5: Writing the PHP Backend LogicLetβs handle the form submission in process.php:
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Database Connection
$conn = mysqli_connect("localhost", "root", "", "contact_db");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO messages (name, email, message) VALUES ('$name', '$email', '$message')";
if (mysqli_query($conn, $sql)) {
echo "Message sent successfully!";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
}
?>
β This script connects to MySQL, inserts data into the messages table, and displays a success message.
β οΈ Tip: Always validate and sanitize input in real-world applications to prevent SQL injection and XSS attacks.
Hereβs how to make your form more secure and user-friendly:
- Input Validation: Use filter_var() to validate emails.
- Prepared Statements: Replace direct SQL queries with mysqli_prepare().
- ReCAPTCHA Integration: Prevent spam submissions.
- Success/Error Feedback: Redirect users with clean alert messages.
Example snippet using prepared statements:
$stmt = $conn->prepare("INSERT INTO messages (name, email, message) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $name, $email, $message);
$stmt->execute();
By learning these secure methods, students at AI Computer Classes, Indore gain confidence in developing real-world PHP applications safely.
Now itβs time to test your project locally:
- Start your XAMPP/WAMP server.
- Place your project folder inside the htdocs directory.
- Open a browser and go to:
http://localhost/contact-form/
- Fill out the form and click Send Message.
- Check the MySQL table to verify that the data is stored successfully.
This step gives you hands-on confidence in handling real backend processes.
π‘ Start Your Coding Journey with AI Computer Classes β Indore!
Master PHP, MySQL, JavaScript, and more with guided mentorship and live projects.
π Apply now at AI Computer Classes
π Old Palasia, Indore
π¬ Step 8: Adding Email Notification (Optional)You can enhance your form by sending an email whenever a message is submitted.
$to = "your-email@example.com"; $subject = "New Contact Form Submission"; $body = "Name: $name\nEmail: $email\nMessage: $message"; $headers = "From: $email"; mail($to, $subject, $body, $headers);
This feature ensures that every submission instantly reaches your inbox.
Creating a Contact Form using PHP and MySQL is one of the most valuable beginner projects for understanding how web applications communicate with databases. It teaches you real-world skills β from frontend design to backend integration and database handling.
At AI Computer Classes β Indore, learners practice these projects step-by-step to build confidence and become job-ready in Web Development, Full-Stack Programming, and Database Management.
So what are you waiting for? β¨
Start coding, keep experimenting, and bring your ideas to life!
π Contact AI Computer Classes β Indore
β Email: hello@aicomputerclasses.com
π± Phone: +91 91113 33255
π Address: 208, Captain CS Naidu Building, near Greater Kailash Road, opposite School of Excellence For Eye, Opposite Grotto Arcade, Old Palasia, Indore, Madhya Pradesh 4