title: PHP - Checking for Duplicate Entry
body: <p>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".</p>
  
  <p>The Entry Form code is as follows:</p>
  
  <pre><code>  &lt;form action="mailer2.php" method="POST"&gt;
      &lt;div&gt;
          &lt;p class="auto-style1" style="width: 408px"&gt;Newsletter Sign-Up Form&lt;/p&gt;
          &lt;p&gt;First Name&lt;/p&gt;
          &lt;input name="firstname" type="text"&gt; &lt;br&gt; &lt;/div&gt;
          &lt;div&gt;
          &lt;p&gt;Last Name&lt;/p&gt;
          &lt;input name="lastname" type="text"&gt;
          &lt;br&gt;
          &lt;/div&gt;
          &lt;p&gt;E-Mail&lt;/p&gt;
          &lt;input name="email" type="text"&gt;
          &lt;br&gt;
          &lt;/div&gt;
          &lt;div&gt;
          &lt;p&gt;What are your interests"&lt;/p&gt;&lt;br&gt;
          &lt;input type="checkbox" name="activity[]" value="run"&gt;I enjoy running&lt;br&gt;
          &lt;input type="checkbox" name="activity[]" value="bike"&gt;
          I enjoy mountain biking&lt;br&gt;
  
          &lt;input type="checkbox" name="activity[]" value="hike"&gt;I enjoy hiking&lt;br&gt;
          &lt;/div&gt;
          &lt;div&gt;
      &lt;input name="submit" type="submit" value="Send!"&gt; &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>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</p>
  
  <pre><code>    &lt;?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 =&gt; $value) {
          //echo $key. ' = ' . $value . '&lt;br&gt;';
          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 = "someone@example.com";
  $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
  
  }
  
  ?&gt;
  &lt;?php
  // close connection
  mysql_close();
  ?&gt;
  </code></pre>
  
  <p>(edit - show form html)</p>