Reputation: 21
I have a simple entry form which includes an email address. I want to check for a duplicate (which I can do) but what I'm struggling with is how to get the entry form to indicate it is a duplicate "Please Try Again".
The Entry Form code is as follows:
<form action="mailer2.php" method="POST">
<div>
<p class="auto-style1" style="width: 408px">Newsletter Sign-Up Form</p>
<p>First Name</p>
<input name="firstname" type="text"> <br> </div>
<div>
<p>Last Name</p>
<input name="lastname" type="text">
<br>
</div>
<p>E-Mail</p>
<input name="email" type="text">
<br>
</div>
<div>
<p>What are your interests"</p><br>
<input type="checkbox" name="activity[]" value="run">I enjoy running<br>
<input type="checkbox" name="activity[]" value="bike">
I enjoy mountain biking<br>
<input type="checkbox" name="activity[]" value="hike">I enjoy hiking<br>
</div>
<div>
<input name="submit" type="submit" value="Send!"> </div>
</form>
The PHP code: This is where I am stuck...how do I get back to my form and note below the email address it is a duplicate
<?php
$hostname = "hostname";
$username = "Username";
$password = "password";
$dbname = "Tablename";
$TableName = "MemberInfo";
// Check connections
mysql_connect($hostname, $username, $password) or die("cannot connect");
mysql_select_db("$dbname")or die("cannot select DB");
// Get values from form
$fname=$_POST['firstname'];
$lname=$_POST['lastname'];
$email=$_POST['email'];
$FullName = $fname . " " . $lname;
$activity=$_POST['activity'];
$run = 0;
$bike = 0;
$hike = 0;
//Check for Duplicate Email
$result = mysql_query("SELECT * FROM MemberInfo WHERE Email='$email'");
$DupCheck = mysql_num_rows($result);
if ($DupCheck) {
echo "Email already exists ... please try again.";
//header('Location: NewsletterSignUp.html');
exit;
//trigger_error('Email Already Exists.', E_USER_WARNING);
}
if (isset($_POST['activity'])) {
$activity = $_POST['activity'];
foreach($activity as $key => $value) {
//echo $key. ' = ' . $value . '<br>';
if ($value == 'run') {
$run = 1;
}
if ($value == 'bike') {
$bike = 1;
}
if ($value == 'hike') {
$hike = 1;
}
}
} else {
echo 'Nothing';
}
// Insert data into mysql
$sql="INSERT INTO $TableName (FirstName, LastName, FullName,
Email, Run, Bike, Hike) VALUES('$fname', '$lname',
'$FullName', '$email', '$run',
'$bike', '$hike')";
$result=mysql_query($sql);
// if successful insert data into database, displays message "Successful".
if($result){
header('Location: Confirmation.html');
}
else {
die('Invalid query: ' . mysql_error());
}
if(isset($_POST['submit'])) {
// Send email
$to = "[email protected]";
$subject = "Newsletter Sign-Up";
// data the visitor provided
$fname_field = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
$lname_field = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
$email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
foreach($_POST['activity'] as $value) {
$activity_field .= "Checked: $value\n";
}
//constructing the message
$body = " From: $fname_field $lname_field\n\n
E-Mail: $email_field\n\n$activity_field";
// ...and away we go!
mail($to, $subject, $body);
} else {
// handle the error somehow
}
?>
<?php
// close connection
mysql_close();
?>
(edit - show form html)
Upvotes: 2
Views: 2610
Reputation: 20286
The easiest way would be making email field as UNIQUE with index, then it will be no possible to add 2 same e-mail fields. The other way is to make select with this mail and check if there is record with this field.
Also don't use mysql_* functions they are old and will be removed in future.
here is more about unique index http://dev.mysql.com/doc/refman/5.0/en/create-index.html
In my personal experience I like to put a lot of things into database, if there is something that can be checked via database, why don't use it?
Make change into your Db
ALTER TABLE $TABLE
ADD UNIQUE INDEX Email(Email);
or
CREATE UNIQUE INDEX unique_email
ON $TABLE (Email)
adding new row where there is already email will return error or if you use pdo it will throw exception. Something like:
ERROR 1062 (23000): Duplicate entry 'xxx' for key 1.
Upvotes: 2