Basic User Subscription with HTML, PHP, and MySQL

Basic User Subscription with HTML, PHP, and MySQL

If you need to collect info on your website via a form, this is how I do it. I will show a simple email subscription here, but you can do it with any form with any number of fields. I will also leverage an .htaccess file to handle environment variables in a secure manner.

Step 1: Database Preparation

Before diving in, make sure you have a MySQL database ready. Construct a table named subscribers and include a column named email.

CREATE TABLE subscribers (
    id INT AUTO_INCREMENT PRIMARY KEY,
    email VARCHAR(255) NOT NULL
);        

Step 2: the HTML Form

Here's a sample HTML form for getting the user’s email address. You can find html and css codes online to get started and change it as you wish. I took this form from here: https://uiverse.io/guilhermeyohan/fresh-newt-22

The form gets the field with "email" tag and sends it to "subscribe.php" using "post" method.

<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User Subscription</title>
</head>
<body>
    <div class="subscribe">
        <p>SUBSCRIBE</p>
        <form action="subscribe.php" method="post" id="subscriptionForm"> 
            <input placeholder="Your e-mail" class="subscribe-input" name="email" type="email" required>
            <br>
            <button class="submit-btn" type="submit">SUBMIT</button>
        </form>
    </div>
</body>
</html>        

Step 3: PHP Script

The following PHP script "subscribe.php" gets the "email" field submitted from the form and inserts it into the database. First, the username, password, and database are taken from environment variables stored in the ".htaccess" file. Then, a connection is made to the database and the new email is inserted in the table named subscribers under email column. In the end it redirects the user to a new page.

<!-- subscribe.php -->
<?php
$servername = "localhost";
$username = getenv('dbuser');
$password = getenv('dbpass');
$dbname = getenv('dbname');

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

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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $email = $_POST["email"];

    $stmt = $conn->prepare("INSERT INTO subscribers (email) VALUES (?)");
    $stmt->bind_param("s", $email);

    if ($stmt->execute()) {
        echo 'Subscription successful!';
    } else {
        echo "Error: " . $stmt->error;
    }

    $stmt->close();
}

$conn->close();

header("Location: thankyou.html");
?>        

Step 4: Configuring .htaccess

Store database credentials in the ".htaccess" file to establish the environment variables for more security.

# .htaccess
SetEnv dbuser 'username'
SetEnv dbpass 'password'
SetEnv dbname 'database'        

Using these simple steps, you can handle any form submissions, process the data using PHP, and store it in a MySQL database. Remember, the employment of environment variables in the .htaccess file introduces an additional layer of security to your database credentials.

要查看或添加评论,请登录

Ali AZARY的更多文章

社区洞察

其他会员也浏览了