These aren't working. The only part that is working is the subscribe. For the trying to subscribe after you have already subscribed you just get added again, and for the unsubscribe, you get a message that the email address cannot be found, even though it is in the database.
Here is the code:
Code: Select all
<?php
//set up table and database names
$db_name = "mailinglist";
$table_name = "subscribers";
//determine if they need to see the form or not
if ($_POST[op] !="ds") {
//create form block
$text_block="
<form method=\"post\" action=\"$_SERVER[PHP_SELF]\" id=\"contact\" />
<fieldset>
<legend>Subscribe to my newsletter</legend>
<input type=\"hidden\" name=\"op\" value=\"ds\">
<label><strong>Your email address:</strong></label><br />
<input type=\"text\" name=\"email_addr\" size=\"25\" maxlength=\"100\" /><br /><br />
<label><strong>Action</strong></label><br />
<input type=\"radio\" name=\"action\" value=\"sub\" checked=\"checked\">subscribe<br />
<input type=\"radio\" name=\"action\" value=\"unsub\" checked=\"checked\">unsubscribe<br />
<p><input class=\"submit\" src=\"/images/submit.gif\" alt=\"Submit\" type=\"image\" name=\"submit\" value=\"submit\" />
</fieldset>
</form>";
} else if ( ($_POST[op] =="ds") && ($_POST[action] == "sub")) {
//trying to subscribe; validate email address
if ($_POST[email_addr] ==" ") {
header("Location:http://www.inspired-evolution.com/manage.php");
exit;
}
//connect to server and select database
$connection = @mysql_connect("localhost", "username", "password")
or die(mysql_error() );
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
//check that email is not already in list
$check = "select email_addr from $table_name
where email_addr = '$_POST[email_addr]' ";
$check_result = @mysql_query($check, $connection) or die(mysql_error());
$check_sum = mysql_num_rows($check_result) ;
//get number of results and do action
if ($check_num < 1) {
//add record
$sql = "insert into $table_name
values( ' ', '$_POST[email_addr]' , now() )";
$result = @mysql_query($sql, $connection) or die(mysql_error() );
$text_block = "<p>Thanks for signing up for my newsletter! <br /> I will be sending this newsletter out monthly once I get a fair number of subscribers.</p>";
} else {
//print failure message
$text_block = "<p>We show you are already in our email database!</p>";
}
} else if ( ($_POST[op] =="ds") && ($_POST[action] == "unsub")) {
//trying to unsubscribe; validate email address
if ($_POST[email_addr] == "") {
header("Location:http://www.inspired-evolution.com/manage.php");
exit;
}
//connect to server and select database
$connection = @mysql_connect("localhost", "username", "password")
or die(mysql_error() );
$db = @mysql_select_db($db_name, $connection) or die(mysql_error( ) );
//check that email is in list
$check ="select id, email_addr from $table_name
where email_addr = ' $_POST[email_addr]' " ;
$check_result = @mysql_query($check, $connection) or die(mysql_error() ) ;
$check_num = mysql_num_rows($check_result);
//get the number of results and do action
if ($check_num < 1) {
//print failure message
$text_block = "<p>Couldn't locate your email address!</p>
<p>You haven't been unsubscribed because the email you entered is not in my databate.</p>";
} else {
//unsubscribe the address
$id= @mysql_result($check_result, 0, "id");
$sql = "delete from $table_name where id = '$id' ";
$result = @mysql_query($sql, $connection) or die (mysql_error() ) ;
$text_block = "<p>You have successfully unsubscribed!</p>" ;
}
}
?>
<html>
<head>
<title>Subscribe/unsubscribe</title>
</head>
<body>
<?php echo "$text_block"; ?>
</body>
</html>I am not getting any errors, just doesn't work.
anyone wishing to check it out feel free to do so.
the page is http://www.inspired-evolution.com/manage.php
let me know what I am doing wrong!