Page 1 of 1

Problems with newsletter subscribe/unsubscribe script

Posted: Wed Apr 26, 2006 10:25 am
by bruceg
I am trying to put together a newsletter list where people can subscribe and unsubscribe. The script is supposed to look into the database and see if someone has already subscribed and alert you and also look into the database to find an email address and delete it .

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>
in the mysql database I have a table with 3 fields one called 'ID' with type 'int' set to 'auto incrementing', another called email_addr with the type 'varchar' and the third called date_added with type 'date'.

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!

Posted: Wed Apr 26, 2006 11:19 am
by John Cartwright
I speak only for myself, but I can't/won't try to read your code since your nested if and elseif' arn't even indented properly. Makes tough to debug your code. Please, help us help you.

Posted: Wed Apr 26, 2006 2:55 pm
by bruceg
Sorry bout that. I really do want to be helped. Hope this is better:

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 (mysql_error () ) ;
		
		$text_block = "<p>You have successfully unsubscribed!</p>" ;
	
	}

}

?>
<html>
<head>
<title>Subscribe/unsubscribe</title>
</head>
<body>
<?php echo "$text_block"; ?>


</body>

</html>
Jcart | This is what I mean by proper indentation