Subscribe/Unsubscribe to a Mailing List

The following PHP scripts shows how to maintain a mailing list. the users are asked to subscribe/unsubscribe to a mailing list and the entire mails are stored in the database.

The user has to type his email id and his/her name and select the radio button subscribe/unsubscribe. Since one email ids are unique, the email id field is a primary key.

Database Queries:

create database dummy;

create table mail(id varchar(100) primary key, name varchar(100))

HTML Form:

<FORM method =post action=sub.php>

<CENTER>

<FONT FACE = “ALGERIAN” SIZE = 6><BR><BR><B><u>SUBSCRIBER / UNSUBSCRIBER </U></B></FONT><BR><BR><BR><BR>

<FONT SIZE = 4><B>Enter E-Mail ID:</B></FONT><INPUT TYPE = text NAME = eid><BR><BR>

<FONT SIZE = 4><B>Name:</B></FONT><INPUT TYPE = text NAME = ename><BR><BR>

<INPUT TYPE = “radio” name = “radio” value = “Radio1″ > Subscriber

<INPUT TYPE = “radio” name = “radio” value = “Radio2″ > Unsubscriber <BR><BR>

<INPUT TYPE = submit VALUE = submit>

<INPUT TYPE = reset VALUE = reset>

</CENTER>

</FORM>

<FORM method =post action=sub.php>

<CENTER>

<FONT FACE = “ALGERIAN” SIZE = 6><BR><BR><B><u>SUBSCRIBER / UNSUBSCRIBER </U></B></FONT><BR><BR><BR><BR>

<FONT SIZE = 4><B>Enter E-Mail ID:</B></FONT><INPUT TYPE = text NAME = eid><BR><BR>

<FONT SIZE = 4><B>Name:</B></FONT><INPUT TYPE = text NAME = ename><BR><BR>

<INPUT TYPE = “radio” name = “radio” value = “Radio1″ > Subscriber

<INPUT TYPE = “radio” name = “radio” value = “Radio2″ > Unsubscriber <BR><BR>

<INPUT TYPE = submit VALUE = submit>

<INPUT TYPE = reset VALUE = reset>

</CENTER>

</FORM>

// Sub.php

<?php

$u = $_POST['eid'];

$p1 = $_POST['ename'];

$p2 = $_POST['radio'];

$con=mysql_connect(”localhost”,”root”,”") or die(”DB connection Failed”);

$db=mysql_select_db(”dummy”,$con) or die(mysql_error());

$query1 = “select * from mail where id = ‘$u’ “;

$query2 = “insert into mail values(’$u’,'$p1′)”;

$query3 = “delete from mail where id = ‘$u’”;

if ($p2 == “Radio1″)

{

$r1 = mysql_query($query1) or die (”Query1 Failed”);

$num = mysql_num_rows($r1);

if ($num == 1)

{

echo “Hai!$u You are already subscribed”;

}

else

{

$r2 = mysql_query($query2) or die (”Insertion query Failed”);

echo “Hai!$u.Thanks for signing up”;

}

}

else if ($p2 == “Radio2″)

{

$r1 = mysql_query($query1) or die (”Query1 Failed”);

$num = mysql_num_rows($r1);

if ($num == 1)

{

$r3 = mysql_query($query3) or die (”Deletion query Failed”);

echo “Hai! $u ur unsubscribed”;

}

else

{

echo “Hai! $u couldnt find ur name in the list”;

}

}

mysql_close($con);

?>

Comments

Popular Posts