Jump to content

[SOLVED] need to check duplicated emails for email sign up form


bradkenyon

Recommended Posts

I have this code that adds email addresses to a table, and I want to check to see if the email address is already in the table, how would I go about that w/ the current code?

 

Any help is appreciated.

 

include('db.php');
if($_POST)
{
	$fname=$HTTP_POST_VARS['fname'];
	$lname=$HTTP_POST_VARS['lname'];
	$email=$HTTP_POST_VARS['email'];

	$query = mysql_query("INSERT INTO maillist (fname, lname, email) VALUES('$fname', '$lname', '$email')");

	result - mysql_query($query);
	if($result)
		echo mysql_affected_rows().' maillist inserted into database.';

?>		
	<h2 align="center">Thank You for signing up, you'll now receive festival updates!</h2>
<?
}
else
{
?>
	<span class="heading">Sign Up</span>
	<p>
	<form method="post" action="<?=$_SERVER["PHP_SELF"]?>">
	<table width="100%"  border="0" cellpadding="5" cellspacing="0" bgcolor="#CAD7BB">
	<tr>
		<td><strong>Email:</strong></td>
		<td><input type="text" name="email"></td>
	</tr>
	<tr>
		<td><strong>First Name:</strong></td>
		<td><input type="text" name="fname"></td>
	</tr>
	<tr>
		<td><strong>Last Name:</strong></td>
		<td><input type="text" name="lname"></td>
	</tr>
	<tr>
		<td colspan="2"><input type="Submit" name="Submit" value="Submit"></td>
	</tr>
	</table>
<?
}
?>

 

Link to comment
Share on other sites

Try this, it checks for an matching email address.

 


include('db.php');
if($_POST)
{
	$fname=$HTTP_POST_VARS['fname'];
	$lname=$HTTP_POST_VARS['lname'];
	$email=$HTTP_POST_VARS['email'];


$checktable = mysql_query("SELECT * FROM maillist WHERE email='$email' "); 

$nrows = mysql_num_rows($checktable);

if ($nrows == 0)

{
	$query = mysql_query("INSERT INTO maillist (fname, lname, email) VALUES('$fname', '$lname', '$email')");

	result - mysql_query($query);
	if($result)
		echo mysql_affected_rows().' maillist inserted into database.';



?>		
	<h2 align="center">Thank You for signing up, you'll now receive festival updates!</h2>
<?


}


}
else
{
?>
	<span class="heading">Sign Up</span>
	<p>
	<form method="post" action="<?=$_SERVER["PHP_SELF"]?>">
	<table width="100%"  border="0" cellpadding="5" cellspacing="0" bgcolor="#CAD7BB">
	<tr>
		<td><strong>Email:</strong></td>
		<td><input type="text" name="email"></td>
	</tr>
	<tr>
		<td><strong>First Name:</strong></td>
		<td><input type="text" name="fname"></td>
	</tr>
	<tr>
		<td><strong>Last Name:</strong></td>
		<td><input type="text" name="lname"></td>
	</tr>
	<tr>
		<td colspan="2"><input type="Submit" name="Submit" value="Submit"></td>
	</tr>
	</table>
<?
}
?>

Link to comment
Share on other sites

it is much faster to use COUNT(), although problemHelpPlease's solution should still work:

 

$nrows = mysql_result(mysql_query("SELECT COUNT(*) FROM maillist WHERE email='$email'"), 0, 0);

 

keep in mind this won't play well if you end up with a query error of any kind.

Link to comment
Share on other sites

Rather than perform two queries, I like to make the column 'unique' in MySQL... you can then check mysql_error() for the string 'Duplicate entry' -> If it's there, you know that the email is already taken.

 

I'm also an efficiency freak, though ;)

Link to comment
Share on other sites

making the column a unique key is also a good way to add a last line of defense against duplicate entries.  i would argue that queries are never the bottleneck in small systems (vs. doing a string search), but i don't think it's really relevant - disco's right, you could do it with one query alone.

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.