How to Create a "Subscribe to Newsletter" With MySQL Using PHP

Save

One of the best ways to keep in touch with visitors to your website is to send out a newsletter. This allows you to send frequent updates to your visitors and encourage them to return to your website. In order to do this, you must have a mechanism for collecting your visitor's names and email addresses. The best way to do this is with a "Subscribe to Newsletter" form, which can be created using MySQL and PHP.

  • Create a MySQL table named "email_list" with the fields "id," "fullname" and "email." The "id" field should be the primary key and an auto-increment field. The "fullname" and "email" fields should be 50 characters and 256 characters long respectively. For convenience, you can create the table with the following query:

    CREATE TABLE email_list (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, fullname VARCHAR(50) NOT NULL, email VARCHAR(256) NOT NULL);

  • Create the HTML form that visitors will use to enter their information. It should contain fields for the visitors' names and email addresses and should send the information to the PHP script that will process the submission, "submit.php." Below is the finished HTML:

    <form action='submit.php' action='POST'>

    <p>Full name: <input type='text' name='fullname' /></p>

    <p>Email: <input type='text' name='email' /></p>

    <input type='submit' value='Sign Up!' />

    </form>

  • Write a PHP script that will process the information that your visitors submit via the form and save it as "submit.php." It will need to connect to your MySQL database and insert an entry with your visitors' names and email addresses. Here is the full PHP code:

    <?php

    $con = mysql_connect("HOST_NAME","USERNAME","PASSWORD") or die('Could not connect: ' . mysql_error());

    mysql_select_db("DATABASE_NAME", $con);

    if(mysql_query("INSERT INTO email_list (fullname, email) VALUES ('".$_POST['fullname']."', '".$_POST['email']."')")) {

    echo "Your email has been added to our list!";

    } else {

    echo "There was an error adding your email to our list. Please try again.";

    }

    mysql_close($con);

    ?>

    You will need to insert your database information into the script where the respective capitalized words appear.

  • Write a PHP script that will retrieve the list of emails from your database so that it can easily be put into the "to" field when sending out a newsletter. It will need to retrieve all the emails from the list and display them in a comma separated list in the format "John Smith <johnsmith@example.com>." Here is the full PHP code:

    <?php

    $con = mysql_connect("HOST_NAME","USERNAME","PASSWORD") or die('Could not connect: ' . mysql_error());

    mysql_select_db("DATABASE_NAME", $con);

    $result = mysql_query("SELECT * FROM email_list");

    while($row = mysql_fetch_array($result)) {

    echo $row['fullname'] . '<' . $row['email'] . '>, ';

    }

    mysql_close($con);

    ?>

    Here, again, you will need to replace your own database information in the script.

References

  • Photo Credit Medioimages/Photodisc/Photodisc/Getty Images
Promoted By Zergnet

Comments

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!