PHP + MYSQL PROJECT TUTORIAL

Build a PHP Contact Form

Learn how to build a complete PHP Contact Form that receives user information, validates the data and stores it inside a MySQL database.

What Are We Building?

In this project, we will create a professional contact form using HTML, CSS, PHP and MySQL.


The user will enter their name, email, phone, subject and message. PHP will process the submitted information and save it into MySQL.

What You Will Learn

HTML Create a professional form using input and textarea elements.
PHP Receive and validate submitted form data.
MySQL Store contact information inside a database.

Requirements

XAMPP installed
Apache server running
MySQL server running
phpMyAdmin
Basic PHP knowledge

Step 1 — Create the Project Folder

Go to your AIFA Coding Lab PHP projects folder and create a folder named:

contact-form

Inside it, create these files:

contact-form/
│
├── tutorial.php
├── index.php
├── submit.php
└── style.css

Step 2 — Create the MySQL Database

Open phpMyAdmin and create a database named:

aifa_php_lab

Then create a table named:

contact_messages

The table should contain these columns:

id
name
email
phone
subject
message
created_at

The SQL used in this project is:

CREATE TABLE contact_messages (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(150) NOT NULL,
    phone VARCHAR(20) NOT NULL,
    subject VARCHAR(200) NOT NULL,
    message TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Step 3 — Create the HTML Form

Open index.php. The form collects five pieces of information:

Name
Email
Phone
Subject
Message

The important part of the form is:

<form action="submit.php" method="POST">

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

    <input type="email" name="email">

    <input type="tel" name="phone">

    <input type="text" name="subject">

    <textarea name="message"></textarea>

    <button type="submit">
        Send Message
    </button>

</form>

The action tells the browser where to send the form data.

The POST method sends the data to the server without placing it in the URL.

Step 4 — Receive Form Data Using PHP

In submit.php, PHP receives the submitted information using the $_POST superglobal.

$name = trim($_POST['name'] ?? '');

$email = trim($_POST['email'] ?? '');

$phone = trim($_POST['phone'] ?? '');

$subject = trim($_POST['subject'] ?? '');

$message = trim($_POST['message'] ?? '');

The trim() function removes unnecessary spaces from the beginning and end of the submitted value.

Step 5 — Validate the Form

Before storing data, we should check whether the user entered valid information.

if (
    $name === '' ||
    $email === '' ||
    $phone === '' ||
    $subject === '' ||
    $message === ''
) {
    // Show error
}

We also check the email address:

if (!filter_var(
    $email,
    FILTER_VALIDATE_EMAIL
)) {
    // Invalid email
}

Step 6 — Connect PHP With MySQL

Our XAMPP MySQL server is running on port 3307.

$host = "127.0.0.1";

$user = "root";

$password = "";

$database = "aifa_php_lab";

$port = 3307;

$conn = new mysqli(
    $host,
    $user,
    $password,
    $database,
    $port
);

The mysqli class creates the connection between PHP and MySQL.

Step 7 — Insert Data Into MySQL

We use a prepared statement to insert the submitted information.

$sql = "
    INSERT INTO contact_messages
    (name, email, phone, subject, message)
    VALUES (?, ?, ?, ?, ?)
";

$stmt = $conn->prepare($sql);

$stmt->bind_param(
    "sssss",
    $name,
    $email,
    $phone,
    $subject,
    $message
);

$stmt->execute();

The five s characters represent five string values.

Step 8 — Understand the Complete Flow

User
→
HTML Form
→
submit.php
→
Validation
→
MySQL

Important Points

Use POST for form submission.
Always validate user input.
Use prepared statements for database queries.
Never trust user input directly.
Keep database credentials secure.

Practice Questions

Add a message length validation.
Add a city field.
Add a country field.
Add a gender field.
Create an admin page to display messages.

Ready to See the Complete Project?

You have learned how the project works. Now open the working project.

🚀 Run Complete Project