Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple subscription

Status
Not open for further replies.

Benluke

Technical User
Apr 2, 2004
112
GB
Hi, i am a newbie :)

ive set up a form so people can subscribe to a newsletter, their name and email is logged into a mysql database.

How do i prevent an email address being logged more than once. eg the same user subscribing multiple times

This is my code im using

<?php
include("connect.inc");
/* grabs the POST variables and puts them into variables that we can use */
$fullName=$_POST['fullName'];
$email=$_POST['email'];


//---------VALIDATION-------->
if($fullName){//----> CHECK input
}
else{
$error.="Please, go back and fill out your full name\n";//----> ERROR if no input
}


if($email){//----> CHECK input
}
else{
$error.="Please, go back and fill out your e-mail address\n";//----> ERROR if no input
}

//-------->ERROR FREE??
if($error==""){
echo "Thank you for subscribing to my FREE Newsletter A receipt of your submission will be e-mailed to you almost immediately.";
//----------------------------------
$mailContent="--------".$fullName." has subscribed to Paso Finos Newsletter. The information given has been logged in the database--------\n"
."Full Name: ".$fullName."\n"
."E-mail: ".$email."\n";
//----------------------------------
$toAddress="#@mysite.co.uk";
$subject="newsletter";
$recipientSubject="Confirming Newsletter subscription";
$receiptMessage = "Thank you ".$fullName." for subscribing to my FREE Newsletter\n\n\nHere is what you submitted to us:\n\n"
."--------CONTACT--------\n"
."First Name: ".$fullName."\n"
."E-mail: ".$email."\n";
//----------------------------------
mail($email, $subject, $receiptMessage,"From:$toAddress");
//----------------------------------
mail($toAddress,$recipientSubject,$mailContent,"From:$email");
//--->echo $mailContent;

//////////////////////////////////////// CONNECT TO MYSQL DB ////////////////////
// OPEN CONNECTION --->

$connection=mysql_connect("$host","$user", "$password") or die("Unable to connect!");

mysql_select_db("$database") or die("Unable to select database!");

// EXECUTE QUERY --->
$query="INSERT INTO mytable (
fullName,
email)
VALUES(
'".$fullName."',
'".$email."')";
//////----->
$result=mysql_query($query) or die("Error in query:".mysql_error());
//if ($result)
//echo mysql_affected_rows()." row inserted into the database effectively.";

// CLOSE CONNECTION --->
mysql_close($connection);

///////////////////////////////////////////////////////////////////////////////////
}
else{

print "Sorry, but the form cannot be sent until the fields indicated are filled out completely - \n";
print "$error\n";
print "\n";
print "\n";
print "Please use your \"Back\" button to return to the form to correct the omissions. Thank you.\n";
}

?>
 
Before performing the insertion, perform a query to see if the email address is already in the database. If it is, don't insert it.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks sleipnir214,

so it would go hear??

// EXECUTE QUERY --->
$query="INSERT INTO mytable (
fullName,
email)
VALUES(
'".$fullName."',
'".$email."')";
//////----->
$result=mysql_query($query) or die("Error in query:".mysql_error());
//if ($result)
//echo mysql_affected_rows()." row inserted into the database effectively.";

Could you show me how i would achieve this, i really am struggling to find the right coding.

im totally new to this as this is my first attempt at using a database

Thanks for any further advice

Ben
 
Right where your "//EXECUTE QUERY" comment

I might attempt to do something like:

Code:
$query = "SELECT count(*) from mytable where email = '" . $email . "'";

$result = mysql_query ($query);

if ($result == TRUE)
{
	list ($record_count) = mysql_fetch_array($result);

	if ($record_count != 0)
	{
		$query="INSERT INTO mytable (fullName, email) VALUES('" . $fullName . "', '" . $email . "')"; 

		$result=mysql_query($query) or die("Error in query:".mysql_error()); 
	
		if ($result == TRUE)
		{
			//print "email address inserted successfully" message
		}
		else
		{
			//print "could not insert email address" message
		}
	}
	else
	{
		//print some kind of "record already in database" message for user
	}
}
else
{
	//print some kind of error message stating there's a database problem
}


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks for that i tried it , but i think im doing somethi wrong. This is what i have

// EXECUTE QUERY --->
$query = "SELECT count(*) from mytable where email = '" . $email . "'";

$result = mysql_query ($query);

if ($result == TRUE)
{
list ($record_count) = mysql_fetch_array($result);

if ($record_count != 0)
{
$query="INSERT INTO mytable (fullName, email) VALUES('" . $fullName . "', '" . $email . "')";

$result=mysql_query($query) or die("Error in query:".mysql_error());

if ($result == TRUE)
{
print "email address inserted successfully - \n";
}
else
{
print "could not insert email address - \n";
}
}
else
{
print "record already in database - \n";
}
}
else
{print "there's a database problem - \n";

}

//////----->
$result=mysql_query($query) or die("Error in query:".mysql_error());
//if ($result)
//echo mysql_affected_rows()." row inserted into the database effectively.";

// CLOSE CONNECTION --->


The same email address is still being inserted into the database.

This comes up when clicking submit

Thank you for subscribing to the FREE Newsletter A receipt of your submission will be e-mailed to you almost immediately.email address inserted successfully -

what am i doing wrong??

Thanks

Ben
 
try this code:

Code:
// first, remove unwated tags and whitespace
$email = strip_tags(trim($email));

// then, query
$result = mysql_query("SELECT count(*) from mytable where email = '" . $email . "'");

// if 0 rows, the user is new!
if (mysql_num_rows($result) == 0) {
     // insert code for inserting the user
  }
else {
    echo "Sorry, your email adress is already in our database<br /> Did you forget your <a href=\"forgot.php?email={$email}&amp;what=login\">login information?</a>";
  }

ps.remember to move all connection-specific variables/includes to the very top of your script, so this query is able to run properly.

you might test this code in it's own file, without the insert query, as then it will be less messy to look at the code.

ps. code is not tested, but I think it should work.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top