DEV Community

Cover image for HTM, CSS Form Connect With MySQL Database
Sudhanshu Gaikwad
Sudhanshu Gaikwad

Posted on

4

HTM, CSS Form Connect With MySQL Database

Introduction

Would you be able to build a web project? Want to collect data from a form and store it in a MySQL database? You’re in the right place! In this guide, I’ll walk you through creating a clean, responsive registration form using HTML and CSS, and connect it to MySQL using PHP (a backend language that bridges the gap). Let’s dive in!


Setup Environment

Before we code, ensure you have:

  • XAMPP/WAMP installed (local server)
  • MySQL database setup
  • VS Code / Sublime Text or any preferred code editor

Create the MySQL Database

  1. Open phpMyAdmin from XAMPP/WAMP.
  2. Create a new database:

CREATE DATABASE user_registration;

Enter fullscreen mode Exit fullscreen mode

Create a users table:

 CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100),
    password VARCHAR(100)
);

Enter fullscreen mode Exit fullscreen mode

Image description

Build the Registration Form (HTML + CSS)

index.html


<!DOCTYPE html>
<html>
<head>
    <title>Registration Form</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div class="container">
        <h2>Register Here</h2>
        <form action="process.php" method="POST">
            <input type="text" name="name" placeholder="Enter your name" required>
            <input type="email" name="email" placeholder="Enter your email" required>
            <input type="password" name="password" placeholder="Enter your password" required>
            <button type="submit">Register</button>
        </form>
    </div>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

style.css


body {
    font-family: 'Arial', sans-serif;
    background: #f4f4f4;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}

.container {
    background: #fff;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    width: 300px;
    text-align: center;
}

h2 {
    color: #333;
}

input {
    width: 100%;
    padding: 10px;
    margin: 10px 0;
    border: 1px solid #ddd;
    border-radius: 5px;
}

button {
    width: 100%;
    padding: 10px;
    background: #4caf50;
    color: #fff;
    border: none;
    border-radius: 5px;
    cursor: pointer;
}

button:hover {
    background: #45a049;
}

Enter fullscreen mode Exit fullscreen mode

Image description

Create the Backend Connection (PHP):

  1. Go to your XAMPP installation directory (C:\xampp on Windows).
  2. Open the htdocs folder: C:\xampp\htdocs and include process.php file.

process.php


 <?php
$servername = "";
$username = "";
$password = "";
$dbname = "";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Get data from the form
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];

// Insert into the database
$sql = "INSERT INTO users (name, email, password) VALUES ('$name', '$email', '$password')";

if ($conn->query($sql) === TRUE) {
    echo "<h2>Registration successful!</h2>";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>


Enter fullscreen mode Exit fullscreen mode

Test It!

  1. Start your XAMPP/WAMP server.
  2. Save all files in the htdocs folder (xampp/htdocs/your_project).
  3. Open the browser and go to http://localhost/your_project/index.html.
  4. Fill the form and hit Register.

Check phpMyAdmin
you’ll see the data saved!

Image description

Postmark Image

"Please fix this..."

Focus on creating stellar experiences without email headaches. Postmark's reliable API and detailed analytics make your transactional emails as polished as your product.

Start free

Top comments (0)

Postmark Image

"Please fix this..."

Focus on creating stellar experiences without email headaches. Postmark's reliable API and detailed analytics make your transactional emails as polished as your product.

Start free

Join the Runner H "AI Agent Prompting" Challenge: $10,000 in Prizes for 20 Winners!

Runner H is the AI agent you can delegate all your boring and repetitive tasks to - an autonomous agent that can use any tools you give it and complete full tasks from a single prompt.

Check out the challenge

DEV is bringing live events to the community. Dismiss if you're not interested. ❤️