Mailing list problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Vinnar
Forum Commoner
Posts: 32
Joined: Tue Feb 01, 2011 11:00 am

Mailing list problem

Post by Vinnar »

Hey guys, so I could some problems with this mailing list script and it is not doing exactly wut i expect it to do

here's the manage_start.html script:

Code: Select all


<html>
<head>
<title>Sub/Unsub</title>
</head>
<body>

<h3> Subscribe or unsubscribe update mailing list</h3>
<p>Cannot find the results you are looking for? You are then welcomed to subscribe this update mailing list, in which you would receive a notificaiton email once the entry, which has the data you are looking for, is updated. <br><br>
	You could also unsubscribe the update mailing list through here if you have previously subscribed.</p>

	<form method=POST action="/php/manage.php">

	<p><b>Your E-mail address:</b></br><br>
	<input type=text name="email" size=40 maxlength=150>
	
	<br><br>
	<input type=radio name="action" value="sub" checked>Subscrbie
	<input type=radio name="action" value="unsub">Unsubscribe
	
	<input type = "hidden" name = "op" value = "ds">
	
	<br><br><input type=submit name="submit" value="Submit form">
	</form>
	

</body>
</html>

and this is the manage.php script:

Code: Select all


<?php

//set up a couple of functions

include('connect.php');

function emailChecker($email){
	global $connect, $check_result;
	//check mail is not already in list
	$check = "select id from users where email = '$email'";
	$check_result = mysqli_query($connect, $check) or die(mysqli_error($connect));
	
}

//Determine if they need to see the form or not
if ( ($_POST[op] == "ds" && ($_POST[action] == 'sub'))){
	//Try to subscribe, so validate email
	echo $_POST[op];
	echo $_POST[action];
	if($_POST[email]=""){
	
	echo $_POST[email];
	hearder("Location: manage_start.php");
	exit();
	} 
	//connect to database
	db();
	
	//check if email is on the list
	
	emailChecker($_POST[email]);
	
	echo emailChecker($_POST[email]);
	//check the number of results to look for duplicates
	if (mysqli_num_rows($check_result)<1){

		//since no records detected, so add this new email
		echo mysqli_num_rows($check_result);
		$sql="INSERT into users (email) values('$_POST[email]')";
		$result = mysqli_query($connect, $sql) or die(mysqli_error($connect));
		echo "<p>Thanks for signing up!!</p>";

	} else {	
	
		//print failure message
		echo mysqli_num_rows($check_result);
		echo "<p>You have already subscribed!</p>";
	}
	
} else if (($_POST[op] == 'ds') && ($_POST[action] == "unsub")){

	//trying to unsubscribe and validate address
	if ($_POST[email] == "") {
	header ("Location: manage_start.html");
	exit();
	} 
	
	db();
	
	emailChecker($_POST[email]);
	
	if (mysqli_num_rows($check_result) <1) {
		//print failure message
		echo mysqli_num_rows($check_result);
		echo "<p>Cannot find your address!</p>
		<p>No action is taken</p>";
		
	} else {
		//unsubscribe address
		echo mysqli_num_rows($check_result);
		$id = mysqli_real_escape_string($connect, $_POST['id']);
		$sql = "DELETE from users where id = '$id'";
		$result = mysqli_query($connect, $sql) or die(mysqli_error($connect));
		echo mysqli_num_rows($check_result);
		echo "<p>You have unsubscribed!</p>";
	}
}

?>

i have tried inserting echo mysqli_num_rows($check_result); right after if (mysqli_num_rows($check_result)<1); for both positive and negative results.

For subscribe, it doesn't matter if i type in a random email address or the one that's actually in the database, the following result always shows up:

dssub8

You have already subscribed!

What is this "dssub8" coming from ? is it related to the "ds" value i assigned?



As for the unsubscribe, it works ok if i type a random email, the result would be:

0

Cannot find your address!

No action is taken

and of coz the "0" is the result of echo mysqli_num_rows($check_result); right after if (mysqli_num_rows($check_result) <1)


but if i wanna delete the actual email address, which is from database, echo shows this:


11

You have unsubscribed!

And the number "11" keeps showing up when I try several DISTINCT emails even tho all the email addresses in database are DISTINCT.


please help me out i am so confused :?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Mailing list problem

Post by social_experiment »

Code: Select all

echo $_POST[op];
echo $_POST[action];
Here is your dssub. Your $_POST['op'] and $_POST['action'] values are 'ds' and 'sub' respectively. Rm those two lines.

Code: Select all

echo mysqli_num_rows($check_result);
This is probably where your 8 comes from.

You create a function to count the number of rows, but outside the function you call values used inside the function.

Code: Select all

function emailChecker($email){
        global $connect, $check_result;
        //check mail is not already in list
        $check = "select id from users where email = '$email'";
        $check_result = mysqli_query($connect, $check);
        $rows = mysqli_num_rows($check_result);
        return $rows;
}

//Determine if they need to see the form or not
if ( ($_POST[op] == "ds" && ($_POST[action] == 'sub'))){
        //Try to subscribe, so validate email
        echo $_POST[op];
        echo $_POST[action];
        if($_POST[email]=""){
        
        echo $_POST[email];
        hearder("Location: manage_start.php");
        exit();
        } 
        //connect to database
        db();
        
        //check if email is on the list
        
        $result = emailChecker($_POST[email]);
        // why echo this ??
        echo emailChecker($_POST[email]);

        //check the number of results to look for duplicates
        if ($result < 1){
                //since no records detected, so add this new email
                echo mysqli_num_rows($check_result);
                $sql="INSERT into users (email) values('$_POST[email]')";
                $result = mysqli_query($connect, $sql) or die(mysqli_error($connect));
                echo "<p>Thanks for signing up!!</p>";

        } else {       
                //print failure message
                // rm this echo.
                echo mysqli_num_rows($check_result);
                echo "<p>You have already subscribed!</p>";
        }
        
}
// rest of your code

Code: Select all

echo mysqli_num_rows($check_result);
If you keep echoing things to the browser you are bound to see them somewhere. :)
Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Vinnar
Forum Commoner
Posts: 32
Joined: Tue Feb 01, 2011 11:00 am

Re: Mailing list problem

Post by Vinnar »

Thx for the reply and i have edited my manage.php as follows:

Code: Select all


<?php

//set up a couple of functions

include('connect.php');

if ($_POST['email'] = null){
	header("Location: manage_start.html");
	exit();
}

function emailChecker($email){
	global $connect, $check_result;
	//check mail is not already in list
	db();
	$check = "select id from users where email = '$email'";
	$check_result = mysqli_query($connect, $check) or die(mysqli_error($connect));
	if (mysqli_num_rows($check_result)<1){
	
		return true; //meaning this user doesn't exist in db
	} else {
		
		return false; //meaning this user exists in db
	}
	
function deleteEmail($email){

	$id = mysqli_real_escape_string($connect, $_POST['id']);
	$sql = "DELETE from users where id = '$id'";
	$result = mysqli_query($connect, $sql) or die(mysqli_error($connect));
	echo "<p>You have unsubscribed!</p>";
}

function addEmail($email){
	
	$sql = "INSERT into users(email) values('$_POST[email]')";
	echo "<p>Thanks for signing up!</p>";
}

}

//Determine if they need to see the form or not
if ( ($_POST['op'] == "ds" && $_POST['action'] == 'sub')){
	//Try to subscribe, so validate email
	
	//connect to database
	db();
	//check if email is on the list
	
	$NoUser = emailChecker($_POST[email]);
	
	//check the number of results to look for duplicates
	if ($NoUser){
	
		addEmail($_POST['email']);

	} else {	
		//print failure message
		echo mysqli_num_rows($check_result);
		echo "<p>You have already subscribed!</p>";
	}
	
} else if (($_POST[op] == 'ds') && ($_POST[action] == "unsub")){

	//trying to unsubscribe and validate address
	$NoUser = emailChecker($_POST[email]);
	
	if(!$NoUser){
		deleteEmail($_POST[email]);
	} else {
		echo mysqli_num_rows($check_result);
		echo "<p>Cannot find your address!</p>
		<p>No action was taken.</p>";
	
	}
}
?>

Note that I made the codes tighter and simpler by using 2 new functions, addEmail and deleteEmail

But I'm still getting the problems below...

When i try to subscribe with blank, random email address and/or actual address, the following error comes up:

8

You have already subscribed!

Y does it keep showing 8 even if I leave it as blank ??

As for unsubscribe, the following error occurs (no matter what email i type in):

Fatal error: Call to undefined function deleteEmail() in C:\xampp\htdocs\php\manage.php

And it's pointing directly to deleteEmail($_POST[email]); at the end

Plz help !! :?:
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Mailing list problem

Post by Jade »

Okay so you have a few problems:

Code: Select all

if ($_POST['email'] = null){
        header("Location: manage_start.html");
        exit();
}
This will always set the value of $_POST['email'] to null. A double equals sign checks for the value and a single equals sign is an assignment operator. Instead use:

Code: Select all

if (!isset($_POST['email'])){
        header("Location: manage_start.html");
        exit();
}
Second, the reason you still see the 8 is because you haven't removed your echo statement with the number of rows from here:

Code: Select all

//Determine if they need to see the form or not
if ( ($_POST['op'] == "ds" && $_POST['action'] == 'sub')){
        //Try to subscribe, so validate email
       
        //connect to database
        db();
        //check if email is on the list
       
        $NoUser = emailChecker($_POST[email]);
       
        //check the number of results to look for duplicates
        if ($NoUser){
       
                addEmail($_POST['email']);

        } else {       
                //print failure message
                echo mysqli_num_rows($check_result); //COMMENT OUT OR REMOVE THESE LINES
                echo "<p>You have already subscribed!</p>";
        }
My guess is your deleteEmail is failing because your email address is being set to null.
Post Reply