Mailing List in PHP
This is our 3rd part of our mailing list example, we
will now display all subscribers to our newsletter on
a page , this would be used for admin purposes mainly
and is always useful to have.
Here is the code for the page which we called shownewsletter.php
to display the data
<html>
<head></head>
<body>
<?php
//shownewsletter.php
//shows all current subscribers to our newsletter
//make a connection
$connection = mysql_connect("localhost" ,
"iainhendry" ,"iain06")
or die("cannot make connection");
//select or database
$db = mysql_select_db("email", $connection)
or die("cannot find database");
//create our sql query
$sql_query = "SELECT * FROM email";
//execute query
$result = mysql_query("$sql_query");
//now display the results on the page
echo ("<table border ='1' width = '60'>");
//loop through our email table
while ($rows = mysql_fetch_row($result))
{
//$rows[0] is our name and $rows[1] is our email
echo ("<tr><td>$rows[0]</td><td>$rows[1]</td></tr>");
}
?>
</body>
</html>
N ow our second part is removing a subscriber , this
is usually place at the bottom of the email as a link.The
subscriber will thewn go to a page with a form on it
and enter his name and email address and remove himself/herself
from the list. Firstly lets build a form .
<form action = "removemenow.php"
method = "post">
<table>
<tr>
<td> Name : </td>
<td>
<input type = "text" name = "name2">
</td>
</tr>
<tr>
<td> Email : </td>
<td>
<input type = "text" name = "email2">
</td>
</tr>
<tr>
<td>
<input type = "submit" value = "remove"
name="submit">
</td>
<td>
<input type = "reset" value = "clear"
name="reset">
</td>
</tr>
</table>
</form>
Similar to before so we will not dwell on this too
much , now for the removemenow.php which does the work
of removing the selected e-mail address.
<?php
//removemenow.php
//this script removes an user from the list
$connection = mysql_connect("localhost","iainhendry","iain06");
$db = mysql_select_db("email" , $connection);
$sql_query = "DELETE FROM email WHERE email = '$email2'";
$result = mysql_query("$sql_query");
echo ("You have unsubscribed");
?>
In our next section we will build a form for webmasters
and then send the newletter to all subscribers in our
list.
<Top
| Back
|