How to Create a "Subscribe to Newsletter" PHP Database

Written by steve mcdonnell Google
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Create a "Subscribe to Newsletter" PHP Database
Send a newsletter to your online subscribers. (Jupiterimages/Comstock/Getty Images)

If you have enough information to share with other people, you may decide to create a newsletter to distribute to subscribers on some regular basis, for example once a month. To do this, you need to allow subscribers to provide information such as their name and e-mail address, and sign up for the newsletter. To maintain this information, you should store it in a database. You can use PHP to create and maintain a subscriber database for a periodic newsletter.

Skill level:


  1. 1

    Create a new MySQL database for subscriber information with phpMyAdmin or a similar database tool. Create a new table to store the subscriber information with the same tool or with a SQL command-line tool. For example, type:

    CREATE TABLE 'subscribers' (


    name VARCHAR(60) NOT NULL,

    e-mail VARCHAR(150) NOT NULL


  2. 2

    Create a new PHP file and insert the HTML Web page headers. For example, type:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

    <html xmlns="">


    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>Subscribe to our Newsletter</title>



  3. 3

    Capture user input into PHP variables at the beginning of the PHP script. If no user input is available, set the variables equal to blank. For example, type:


    $name = $_POST["name"] || "";

    $email = $_POST["email"] || "";


  4. 4

    Create the HTML form for users to enter their subscriber information. Set the action equal to the PHP file itself. For example, type:

    <h2>Subscribe to our Newsletter</h2>

    <form action="<?php echo htmlentities($_SERVER["PHP_SELF"]); ?>" method="post">

    <p><strong>Name: </strong><input name="name" type="text" width="50" value="<?php echo $name; ?>" /></p>

    <p><strong>Email: </strong><input name="email" type="text" width="50" value="<?php echo $email; ?>"/></p>

    <p><input type="submit" name="submit" value="Submit" /></p>


  5. 5

    Process the subscriber information only if a name and e-mail address were provided. For example, type:

    if ($name == "" || $e-mail == "") return;

  6. 6

    Connect to the MySQL database and select the subscriptions table. For example, type:

    $dbc = mysql_connect("localhost", "username", "password");

    $dB = mysql_select_db("subscriptions");

  7. 7

    Create a query that returns the subscriber records that match the email address input. If a subscriber record is returned, don't add another to the database. Escape any user input fields to avoid SQL injections. For example, type:

    $query = 'SELECT * FROM subscribers WHERE e-mail = "' . mysql_real_escape_string($e-mail) . '"';

    $result = mysql_query($query) || die("Could not run select query!");

    if (mysql_num_rows($result) > 0) {

    echo $e-mail . " already exists in the subscriber database!";


  8. 8

    Add the record to the database if no subscriber already exists. Escape all user input fields to prevent SQL injections. Close remaining HTML tags. For example, type:

    else {

    $query = 'INSERT INTO subscribers (id, name, e-mail) VALUES (null, "' . mysql_real_escape_string($name) . '", "' . mysql_real_escape_string($e-mail) . '")';

    $result = mysql_query($query) || die("Error adding " . $e-mail . " to the database!");

    echo $name . " at " . $e-mail . " was successfully added to the database!";





Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.