Jump to content

Mailing list problem


tommy168

Recommended Posts

Hi, I am new to this forum and hopefully u guys can help me solve the problem i have in this first post!  ;D

 

So please take a look at this html script 

 

<html>

<head>

<title>Sub/Unsub</title>

</head>

<body>

 

<h1> Subscribe or unsubscribe mailing list</h1>

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

 

<p><b>Your E-mail address:</b></br>

<input type=text name="email" size=40 maxlength=150>

 

<p><b>Action:</b></p>

<input type=radio name="action" value="sub" checked>Subscrbie

<input type=radio name="action" value="unsub">Unsubscribe

 

<p><input type=submit name="submit" value="Submit form"></p>

</form>

 

 

</body>

</html>

 

And this is the manage.php script:

 

<?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));

 

}

 

 

if ( ($_POST[action] == 'sub')){

//Try to subscribe, so validate email

if($_POST=""){

hearder("Location: manage_start.php");

exit();

}

//connect to database

db();

 

//check if email is on the list

 

emailChecker($_POST);

 

//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

$sql="INSERT into users (email) values('$_POST')";

$result = mysqli_query($connect, $sql) or die(mysqli_error($connect));

echo "<p>Thanks for signing up man!!</p>";

 

} else {

 

//print failure message

echo "<p>You have already subscribed!</p>";

}

 

} else if (($_POST[action] == "unsub")){

 

//trying to unsubscribe and validate address

if ($_POST == "") {

header ("Location: manage_start.html");

exit();

}

 

db();

 

emailChecker($_POST);

 

if (mysqli_num_rows($check_result) <1) {

//print failure message

echo "<p>Cannot find your address!</p>

<p>No action is taken</p>";

 

} else {

//unsubscribe address

$id = mysqli_real_escape_string($connect, $_POST['id']);

$sql = "DELETE from users where id = '$id'";

$result = mysqli_query($connect, $sql) or die(mysqli_error());

echo "<p>You have unsubscribed!</p>";

}

}

 

?>

 

So wut i am trying to do in this script is to create a mailing list form for user to subscribe/unsubscribe using email address. When i first run the html script (which includes php script) and subscribe by the first time, it went perfectly fine and it echos "thanks for signing up man!!" but when i try it with the totally DIFFERENT email address, it always says "You have already subscribed!!" Even when i put blank on the address box, it still shows "You have already subscribed!!"

 

Also it's the same deal as unsubscribe, first time i could delete an entry from my database completely, but afterward when i try to delete the exact email addresses from database, it wouldn't work !! It just says "You have unsubscribed!" but when i checked back if the entry has been deleted, the answer is NO ! :confused:

 

I am wondering if this is the problem about $global values under function emailChecker( ). Btw my database connection works absolutely fine.

 

I know this is a long ass post but i need an urgent answer PLZZZZZZZZ  ::)

Link to comment
Share on other sites

echo echo echo. echo values to see what they are.

 

echo $_POST['email'] to make sure it's what you expect.

 

after $check = "select id from users where email = '$email'";

 

echo $check to see if it's what you expect.

 

if mysqli_num_rows($check_result) <1

 

echo mysqli_num_rows($check_result) to see if it's what you expect.

 

basic debugging is called for!

Link to comment
Share on other sites

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.

 

 

Plz help me out i am so confused  :confused:

 

Link to comment
Share on other sites

I have edited my script as follows:

 


<?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); at the end

 

Plz help !!  :confused: :confused: :confused:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.