title: Prevent duplicate data being entered into mysql database
body: <p>I'm trying to make my email subscription service reject emails that already exist within my database so users don't subscribe the same email twice. this is what I have but its not working, any ideas?</p>
  
  <pre><code>&lt;?php 
      if(!isset($_POST['submit'])) 
          exit();
  
      $vars = array('email');
      $verified = TRUE;
      foreach($vars as $v) {
          if(!isset($_POST[$v]) || empty($_POST[$v])) {
              $verified = FALSE;
          }
      }   
      if(!$verified) {
          echo "&lt;p style='color:white; margin-top:25px;'&gt;*Email required*&lt;/p&gt;";
          exit();
      }
      $email = $_POST['email'];
      if($_POST['submit']) echo "&lt;p style='color:white; margin-top:25px;'&gt;*Check your inbox*        &lt;/p&gt;";
  
      // Create connection
      $con=mysqli_connect("mysql.host","user","password","dbname");
  
      // Check connection
      if (mysqli_connect_errno($con))
      {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
  
      $sql="INSERT INTO emails (email)    VALUES  ('$_POST[email]')";
      if (!mysqli_query($con,$sql))
      {
          die('Error: ' . mysqli_error($con));
      }
  
      $query = mysql_query("SELECT * FROM emails WHERE email='$email'",($con));
      if(mysql_num_rows($query) != 1)
      {
          echo "email already exists";
          // redirect back to form and populate with 
          // data that has already been entered by the user
      }
  
  
      mysqli_close($con);
  ?&gt;
  </code></pre>