Skip to content
TheChatArchiveThe Chat Archive
Geminigemini-2.5-pro· GoogleHugging Face LMArena Human Preference 140K

Generate MySQL, PHP, HTMS and CSS backend customer registration

  1. User

    Generate MySQL, PHP, HTMS and CSS backend customer registration

  2. Gemini Assistant

    Of course! Here is a complete, secure, and well-explained backend customer registration system using MySQL, PHP, HTML, and CSS.

    Overview

    This system consists of four main files:

    1. db_connect.php: Handles the connection to your MySQL database.
    2. register.php: The main registration page containing the HTML form and PHP logic to display messages.
    3. process_registration.php: The backend PHP script that validates data, hashes the password, and inserts the new customer into the database.
    4. style.css: The CSS file to make the form look clean and professional.

    This setup separates the presentation (HTML/CSS), the business logic (PHP), and the database connection, which is a good development practice.


    Step 1: MySQL Database and Table Setup

    First, you need a database and a table to store the customer information.

    1. Open your MySQL management tool (like phpMyAdmin).
    2. Create a new database. Let's name it my_app.
    3. Run the following SQL query to create the customers table within the my_app database.

    SQL Code:

    CREATE TABLE `customers` (
      `id` INT(11) NOT NULL AUTO_INCREMENT,
      `first_name` VARCHAR(100) NOT NULL,
      `last_name` VARCHAR(100) NOT NULL,
      `email` VARCHAR(255) NOT NULL,
      `password` VARCHAR(255) NOT NULL,
      `registration_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY (`id`),
      UNIQUE KEY `email` (`email`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
    

    Explanation:

    • id: A unique, auto-incrementing number for each customer (Primary Key).
    • first_name, last_name: Stores the customer's name.
    • email: Stores the customer's email. It is marked as UNIQUE to prevent multiple accounts with the same email address.
    • password: Will store the hashed password. VARCHAR(255) is long enough for modern hashing algorithms. We will never store plain-text passwords.
    • registration_date: Automatically records when the customer signed up.

    Step 2: Project File Structure

    Create a folder for your project (e.g., registration_system) and create the following files inside it:

    /registration_system
    |-- db_connect.php
    |-- register.php
    |-- process_registration.php
    |-- style.css
    

    Step 3: The Code

    Now, let's fill in the content for each file.

    File 1: db_connect.php

    This file establishes a connection to your database. It's kept separate so you can easily reuse it for other features like logging in.

    <?php
    // db_connect.php
    
    // --- Database Credentials ---
    // Replace with your actual database details
    define('DB_SERVER', 'localhost');
    define('DB_USERNAME', 'root'); // Your database username
    define('DB_PASSWORD', '');     // Your database password
    define('DB_NAME', 'my_app');   // The database you created
    
    // --- Attempt to connect to MySQL database ---
    $conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
    
    // --- Check connection ---
    if ($conn->connect_error) {
        // If connection fails, stop the script and display an error.
        die("ERROR: Could not connect. " . $conn->connect_error);
    }
    
    // Optional: Set character set to utf8mb4 for full Unicode support
    $conn->set_charset("utf8mb4");
    ?>
    

    Action: Update DB_USERNAME and DB_PASSWORD with your actual database credentials.

    File 2: style.css

    This stylesheet will make our registration form look presentable.

    /* style.css */
    body {
        font-family: Arial, sans-serif;
        background-color: #f0f2f5;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
        margin: 0;
    }
    
    .container {
        background-color: #fff;
        padding: 2rem;
        border-radius: 8px;
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        width: 100%;
        max-width: 400px;
    }
    
    h2 {
        text-align: center;
        color: #333;
        margin-bottom: 1.5rem;
    }
    
    .form-group {
        margin-bottom: 1rem;
    }
    
    .form-group label {
        display: block;
        margin-bottom: 0.5rem;
        color: #555;
        font-weight: bold;
    }
    
    .form-group input {
        width: 100%;
        padding: 0.75rem;
        border: 1px solid #ccc;
        border-radius: 4px;
        box-sizing: border-box; /* Important for padding and width */
    }
    
    .btn {
        width: 100%;
        padding: 0.75rem;
        background-color: #007bff;
        border: none;
        border-radius: 4px;
        color: white;
        font-size: 1rem;
        font-weight: bold;
        cursor: pointer;
        transition: background-color 0.3s;
    }
    
    .btn:hover {
        background-color: #0056b3;
    }
    
    /* Message Styles */
    .message {
        padding: 1rem;
        margin-bottom: 1rem;
        border-radius: 4px;
        text-align: center;
    }
    
    .message.success {
        background-color: #d4edda;
        color: #155724;
        border: 1px solid #c3e6cb;
    }
    
    .message.error {
        background-color: #f8d7da;
        color: #721c24;
        border: 1px solid #f5c6cb;
    }
    

    File 3: register.php (The HTML Form)

    This is the page the user will see. It includes the HTML form and a small PHP block to display success or error messages passed back from the processing script.

    <?php
    // Start the session to access session variables
    session_start();
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Customer Registration</title>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <div class="container">
            <h2>Register New Customer</h2>
    
            <?php
            // Check for success message
            if (isset($_SESSION['success_message'])) {
                echo '<div class="message success">' . $_SESSION['success_message'] . '</div>';
                // Unset the session variable so it doesn't show again on refresh
                unset($_SESSION['success_message']);
            }
            
            // Check for error messages
            if (isset($_SESSION['error_message'])) {
                echo '<div class="message error">' . $_SESSION['error_message'] . '</div>';
                // Unset the session variable
                unset($_SESSION['error_message']);
            }
            ?>
    
            <form action="process_registration.php" method="POST">
                <div class="form-group">
                    <label for="first_name">First Name</label>
                    <input type="text" id="first_name" name="first_name" required>
                </div>
                <div class="form-group">
                    <label for="last_name">Last Name</label>
                    <input type="text" id="last_name" name="last_name" required>
                </div>
                <div class="form-group">
                    <label for="email">Email Address</label>
                    <input type="email" id="email" name="email" required>
                </div>
                <div class="form-group">
                    <label for="password">Password</label>
                    <input type="password" id="password" name="password" minlength="8" required>
                </div>
                <div class="form-group">
                    <label for="confirm_password">Confirm Password</label>
                    <input type="password" id="confirm_password" name="confirm_password" required>
                </div>
                <button type="submit" class="btn">Register</button>
            </form>
        </div>
    </body>
    </html>
    

    File 4: process_registration.php (The Backend Logic)

    This is the core of the backend. It receives the form data, validates it, and interacts with the database. This script does not produce any visual output; it only processes data and redirects back to the form.

    <?php
    // process_registration.php
    
    // Start the session to store messages
    session_start();
    
    // Include the database connection file
    require_once 'db_connect.php';
    
    // Check if the form was submitted
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        
        // --- Data Collection and Basic Validation ---
        $first_name = trim($_POST['first_name']);
        $last_name = trim($_POST['last_name']);
        $email = trim($_POST['email']);
        $password = $_POST['password'];
        $confirm_password = $_POST['confirm_password'];
    
        // --- Server-Side Validation ---
    
        // 1. Check if passwords match
        if ($password !== $confirm_password) {
            $_SESSION['error_message'] = "Passwords do not match.";
            header("Location: register.php");
            exit();
        }
    
        // 2. Validate email format
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $_SESSION['error_message'] = "Invalid email format.";
            header("Location: register.php");
            exit();
        }
        
        // 3. Check password length
        if (strlen($password) < 8) {
            $_SESSION['error_message'] = "Password must be at least 8 characters long.";
            header("Location: register.php");
            exit();
        }
    
        // --- Check if email already exists ---
        $sql_check = "SELECT id FROM customers WHERE email = ?";
        if ($stmt_check = $conn->prepare($sql_check)) {
            $stmt_check->bind_param("s", $email);
            $stmt_check->execute();
            $stmt_check->store_result();
            
            if ($stmt_check->num_rows > 0) {
                $_SESSION['error_message'] = "An account with this email already exists.";
                header("Location: register.php");
                exit();
            }
            $stmt_check->close();
        }
    
        // --- Hash the Password ---
        // This is a CRITICAL security step.
        $hashed_password = password_hash($password, PASSWORD_DEFAULT);
    
        // --- Prepare an INSERT statement ---
        $sql = "INSERT INTO customers (first_name, last_name, email, password) VALUES (?, ?, ?, ?)";
        
        if ($stmt = $conn->prepare($sql)) {
            // Bind variables to the prepared statement as parameters
            // ssss = string, string, string, string
            $stmt->bind_param("ssss", $first_name, $last_name, $email, $hashed_password);
    
            // Attempt to execute the prepared statement
            if ($stmt->execute()) {
                // Success! Set a success message and redirect
                $_SESSION['success_message'] = "Registration successful! You can now log in.";
                header("Location: register.php");
                exit();
            } else {
                // If execution fails, set a generic error message
                $_SESSION['error_message'] = "Something went wrong. Please try again later.";
                header("Location: register.php");
                exit();
            }
    
            // Close statement
            $stmt->close();
        }
    }
    
    // Close connection
    $conn->close();
    
    ?>
    

    How to Run

    1. Place all four files in the correct structure inside your web server's root directory (e.g., htdocs for XAMPP, www for WAMP).
    2. Make sure your Apache and MySQL servers are running.
    3. Open your web browser and navigate to http://localhost/registration_system/register.php.
    4. Fill out the form and submit it! You should see success or error messages displayed on the same page. Check your customers table in phpMyAdmin to see the newly added record.