title: PHP email subscription validation and check for existing
body: <p>I am creating an email subscription form in PHP and want to check for a valid address as well as if the email is already existing in my database. </p>
  
  <p>My code is connecting to my database and inserting but the validation as well as checking for an existing email are not working. </p>
  
  <p>No matter what I type into my form it inserts it into my database even if I don't type anything. </p>
  
  <p>Here is all of my code: </p>
  
  <pre><code>&lt;form action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;" method="post"&gt;
  
  &lt;fieldset&gt;
  
  &lt;legend&gt;Subscribe to Our Newsletter &amp;nbsp;&lt;/legend&gt;
  
  &lt;?php if ($feedback!='')
  echo('&lt;p&gt;'.$feedback.'&lt;/p&gt;'); ?&gt;
  
  &lt;label&gt;Name: &lt;input name="name" type="text" value="&lt;?php echo $name; ?&gt;" /&gt;&lt;/label&gt;
  
  &lt;label&gt;Email: &lt;input name="email" type="text" value="&lt;?php echo $email; ?&gt;" /&gt;&lt;/label&gt; 
  
  &lt;label&gt;&lt;input type="submit" value="Sign Up!" /&gt;&lt;/label&gt;
  
  &lt;/fieldset&gt;
  &lt;/form&gt;
  
  &lt;?php
  
  
  
  $feedback='';
  if (!$email) {
  
  $feedback .= '&lt;strong&gt;Please enter your email address&lt;/strong&gt;&lt;br /&gt;';
  
  }
  
  if (!$name) {
  
  $feedback .= '&lt;strong&gt;Please enter your name&lt;/strong&gt;&lt;br /&gt;';
  }
  
  
  
  list($username, $mailDomain) = explode("@", $email);
  
  if (!@checkdnsrr($mailDomain, "MX")) {
  
  
  $feedback .= '&lt;strong&gt;Invalid email domain&lt;/strong&gt;&lt;br /&gt;';
  }
  
  
  if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $email)) {
  
  
  $feedback .= '&lt;strong&gt;Your email address doesn\'t appear to be valid - please check and try again';
  
  }
  
  
  
  function cleaninput($value, $DB) {
      if (get_magic_quotes_gpc()) {
          $value = stripslashes( $value );
  }
  return mysql_real_escape_string( $value, $DB );
  
  }
  
  
  $name=$_POST['name'];
  $email=$_POST['email'];
  
  include_once "connect.php";
  
  
  $sql = mysql_query("SELECT * FROM subscribers WHERE email='$email'");
  $numRows = mysql_num_rows($sql);
      if ($numRows&gt;0) {
  
  
  $feedback = '&lt;strong&gt;That email address is already subscribed.&lt;/strong&gt;';
  
  }
  
  $insertresult = mysql_query("INSERT INTO subscribers (name, email) VALUES('$name', '$email')") or die (mysql_error());
  
  
      if ($insertresult) {
          $completed = true;
      }
  
  
  
  if($competed=false) {
  ?&gt;
  
  &lt;form action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt; method="post"&gt;
          &lt;fieldset&gt;
      &lt;legend&gt;Subscribe to OUr Newsletter &amp;nbsp;&lt;/legend&gt;
  &lt;?php 
  
  if ($feedback!='')
      echo('&lt;p&gt;'.$feedback.'&lt;/p&gt;'); ?&gt;
  &lt;label&gt;Name: &lt;input name="name" type="text" value="&lt;?php echo $name; ?&gt;" /&gt;&lt;/label&gt;
  
  &lt;label&gt;Email: &lt;input name="email" type="text" value="&lt;?php echo $email; ?&gt;" /&gt;&lt;/label&gt;
  
  &lt;label&gt;&lt;input type="submit" value="Sign Up!" /&gt;&lt;/label&gt;
  
          &lt;/fieldset&gt;
  &lt;/form&gt;
  
  &lt;?php
  
  }
  else {
  
  
  
  echo('Thanks - you have subscribed to our newsletter successfully. You can unsubscribe at any time by clicking the link at the bottom of each email we send.');
  
  }
  
  
  
  ?&gt;
  </code></pre>
  
  <p>Also the last echo in my script is always there. It is displayed under my my form always. Not sure why that is. Maybe I have it in the wrong place in my code.</p>
  
  <pre><code>else {
  
  
  
  echo('Thanks - you have subscribed to our newsletter successfully. You can unsubscribe at any time by clicking the link at the bottom of each email we send.');
  
  }
  </code></pre>
  
  <p>Thanks!</p>