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.
🎓 Create a Contact Form with PHP and MySQL — Practical GuideCreating 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:
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:
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!
📍 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:
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:
http://localhost/contact-form/
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
Use ChatGPT for Writing and Editing — How-To. Get practical lessons and hands-on examples...