2016-03-27 92 views
1

對不起格式不好,但任何人都可以幫助我。我首先檢查電子郵件是否存在於數據庫中。這是我的問題發生的地方。除$ $ checkemail之外,其他一切正常。檢查MySQL數據庫中是否存在郵件時出錯?

<?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); 
    } 
} 
?> 
+0

您是否收到錯誤消息?你有沒有檢查過'mysqli_error()',即。 'mysqli_query($ link,$ addEmail)或die(mysqli_error($ link));' – Sean

+0

你可以詳細解釋一下嗎? –

+0

@肖恩我真的不知道在哪裏包括。我是一個PHP和MySQL的初學者 –

回答

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; 
?> 
+0

所以'list(,$ user_domain)'沒有問題? (請參閱[這個評論](http://stackoverflow.com/questions/36253759/error-in-checking-if-email-exists-in-mysql-database#comment60137261_36253759)) – Sean

+0

你可以使用這樣的分號表示法嗎?在PHP中?我只是問我不知道 –

+0

@Sean沒有正常工作 –

相關問題