检查MySQL数据库中是否存在电子邮件时出错?

时间:2016-03-27 23:08:05

标签: php mysql

抱歉格式不佳,但任何人都可以帮助我。我首先检查数据库中是否存在该电子邮件。这就是我的问题所在。其他一切正常,除了$ checkemail。

<?php
if (array_key_exists("submit", $_POST)) {
    $link = mysqli_connect("localhost", "charlieroth", "Charlie123", "betausersdb");
   if (mysqli_connect_error()) {
       die("Error Connecting To Database");
   }

   $checkemail = "SELECT `email` FROM `users` WHERE `email` = '".mysqli_real_escape_string($link, $_POST['email'])."'";
   $query = mysqli_query($checkemail);
   if (mysqli_num_rows($query) > 0) {
       // do nothing
   } else {
       if (validateEmail($_POST['email'])) {
           $addEmail = "INSERT INTO `users` (`email`) VALUES ('".mysqli_real_escape_string($link, $_POST['email'])."')";
           mysqli_query($link, $addEmail);
           // add email
       }
   }
}

function validateEmail($useremail) {
    if (!preg_match('/^([a-z0-9\+\_\-\.]+)@([a-z0-9\+\_\-\.]{2,})(\.[a-z]{2,4})$/i', $useremail)) {
        // echo "Invalid Email";
        return false;
    } else {
        $domain = array('umich.edu');
        list(, $user_domain) = explode('@', $useremail, 2);
        return in_array($user_domain, $domain);
    }
}
?>

1 个答案:

答案 0 :(得分:0)

您的第一个mysqli_query中有错误。这样做:

<?php

      function validateEmail($useremail) {
          if (!preg_match('/^([a-z0-9\+\_\-\.]+)@([a-z0-9\+\_\-\.]{2,})(\.[a-z]{2,4})$/i', $useremail)) {
              // echo "Invalid Email";
              return false;
          } else {
              $domain = array('umich.edu');
              list(, $user_domain) = explode('@', $useremail, 2);
              return in_array($user_domain, $domain);
         }
    }   

    $mail = addslashes($_POST['email']);
    if (array_key_exists("submit", $_POST)) :
          $link = mysqli_connect("localhost", "charlieroth", "Charlie123", "betausersdb");
           if ( $link ) :
             $checkemail = "SELECT * FROM 'users' WHERE email  = '$mail' ";
             $query = mysqli_query($link, $checkemail );
             if (mysqli_num_rows($query) > 0) {
                       // do nothing
             } 
             else {
                   if (validateEmail($mail) {
                       $addEmail = "INSERT INTO 'users' (email) VALUES ($mail)";
                       mysqli_query($link, $addEmail);
                       // add email
                   }
            }
          }

           else :
              echo 'Error';

           endif;


    endif;
?>