在数据库中查找现有电子邮件和用户名

时间:2016-07-21 19:39:27

标签: php mysql database email

如果我的 mysql数据库中已存在电子邮件或用户名,而其他用户正在注册,那么我可以停止注册进程并且可能建议新用户在数据库中不存在另外的2/3新用户名。

所以我尝试了解决方法,我只能检查电子邮件是否已经存在于数据库中,同时我需要在数据库中检查用户名和电子邮件并打印两者 $ error 分开。我的意思是,如果电子邮件存在且用户名不存在,它将只打印错误电子邮件,并且用户名也是如此。

我希望有人了解我。

    <?php
if(isset($_POST['register'])) {
  $username = $_POST['username'];
  $full_name = ucwords($_POST['full_name']);
  $email = $_POST['email'];
  $password = trim($_POST['password']);
  $time = time();
  $age = $_POST['age'];
  $gender = $_POST['gender'];

  // Geolocation
  $longitude = $_SESSION['longitude'];
  $latitude = $_SESSION['latitude'];

  $geo_info = $geo->getInfo($latitude,$longitude);
  $city = $geo_info['geonames'][0]['name'];
  $country = $geo_info['geonames'][0]['countryName'];

  $check_d = $db->query("SELECT username, email from users WHERE username = '$username' OR email = '$email'");
  $check_d = $check_d->num_rows;
  if($check_d == 0) {
    $db->query("INSERT INTO users (profile_picture,username,full_name,email,password,registered,credits,age,gender,ip,country,city,longitude,latitude) VALUES ('default_avatar.png','$username','$full_name','$email','".$auth->hashPassword($password)."','$time','100','$age','$gender','$ip','".$country."','".$city."','".$longitude."','".$latitude."')");
    setcookie('justRegistered', 'true', time()+6);
    setcookie('mm-email',$email,time()+60*60*24*30,'/');
    header('Location: '.$domain.'/people');
}
else { $error = 'Username or password already exist, Try Another';

  }
  }
if($auth->isLogged()) {
  $first_name = $system->getFirstName($_SESSION['full_name']);
  $logged_in_user = header('Location: '.$domain.'/people');
}

$users = $db->query("SELECT * FROM users ORDER BY RAND() LIMIT 7");

?>

2 个答案:

答案 0 :(得分:1)

通常,您希望在数据库级别处理此类约束。使用MySQL,您可以在用户名和电子邮件列中添加unique索引。输入重复项后,查询将失败。

然后,您可以检查是否发生重复并相应地处理它。

try {

   // Attempt to insert user here

} catch (Exception $e) {

   // It failed! Check if username/email exists so we can confirm the exception is this
   // The following is pseudocode -- you will need to write your own
   if (username_exists() || email_exists() {

      // Generate alternative names
      $names = generateNames($username);

      // Send this to your view or however you are displaying info
      echo("Name is taken!<br>You can try one of these:<br>");
      echo("<ul>");

      foreach ($names as $name) {
         echo("<li>$name</li>");
      }

      echo("</ul>");

   }
}

答案 1 :(得分:0)

您可以通过在分开的查询中检查用户名和电子邮件来完成此操作。

在我发布的实现中,我首先检查邮件,然后检查数据库中的用户名。如果两者都已存在于数据库中,则稍后的错误消息(对于用户名)将覆盖该电子邮件的消息。您应该更改它,以便向用户提供有关数据集无法插入表格的更准确的反馈。

在进行任何检查之前,我正在初始化变量$error AS null,因为如果其中一个约束检查失败,它将被覆盖,我可以简单地检查它是否仍为空判断是否应该执行insert

注意:您不应该使用此脚本,因为它很容易受到sql注入的攻击。请参阅此主题以了解您应该更改的内容:How can I prevent SQL injection in PHP?

$error = null;
$check_mail = $db->query("SELECT id FROM users WHERE email='" . $email . "'");
if ($check_mail->num_rows > 0) {
    $error = 'Email already exists';
}
$check_username = $db->query("SELECT id FROM users WHERE username='" . $username . "'");
if ($check_username->num_rows > 0) {
    $error = 'username already exists';
}
if($error === null){
    $db->query("INSERT INTO users (profile_picture,full_name,email,password,registered,credits,age,gender,ip,country,city,longitude,latitude) VALUES ('default_avatar.png','$full_name','$email','" . $auth->hashPassword($password) . "','$time','100','$age','$gender','$ip','" . $country . "','" . $city . "','" . $longitude . "','" . $latitude . "')");
    setcookie('justRegistered', 'true', time() + 6);
    setcookie('mm-email', $email, time() + 60 * 60 * 24 * 30, '/');
    header('Location: ' . $domain . '/people');
}