AiComputerClasses 3 days ago
aicomputerclasses #programming

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.

🎓 Create a Contact Form with PHP and MySQL — Practical Guide

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.


🧩 Step 1: Understanding the Purpose of a Contact Form

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 Folder

To 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.


🧰 Step 3: Designing the HTML Contact Form

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.


🗄️ Step 4: Creating the MySQL Database

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 Logic

Let’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.


🔒 Step 6: Enhancing Security and Validation

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.


🌐 Step 7: Testing Your Contact Form

Now it’s time to test your project locally:

  1. Start your XAMPP/WAMP server.
  2. Place your project folder inside the htdocs directory.
  3. Open a browser and go to:
http://localhost/contact-form/
  1. Fill out the form and click Send Message.
  2. 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.


🧭 Conclusion

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

Tips & Tricks: Create Branded Templates for Clients using TradingView

Tips & Tricks: Create Branded Templates for Clients using TradingView

1761665883.png
AiComputerClasses
3 days ago
Persuasive Speaking Techniques

Persuasive Speaking Techniques

1761665883.png
AiComputerClasses
3 days ago
Essentials: Intro to Computer Vision with Python

Essentials: Intro to Computer Vision with Python

1761665883.png
AiComputerClasses
3 days ago

🤖 Use ChatGPT for Writing and Editing — How-To

Use ChatGPT for Writing and Editing — How-To. Get practical lessons and hands-on examples...

1761665883.png
AiComputerClasses
3 days ago
Practical Guide: Automate Tasks with Cron and Python Scripts with Power BI

Practical Guide: Automate Tasks with Cron and Python Scripts with Powe...

1761665883.png
AiComputerClasses
3 days ago