我如何验证我的电子邮件是否已经存在?

if(count($_POST)>0) { /* Form Required Field Validation / foreach($_POST as $key=>$value) { 
if(empty($_POST[$key])) {
 $message = ucwords($key) . " field is required"; break; } } / Password Matching Validation */ 

if($_POST['password'] != $_POST['confirm_password']){ $message = 'Passwords should be same '; }

/* Email Validation */ if(!isset($message)) { if (!filter_var($_POST["userEmail"], FILTER_VALIDATE_EMAIL)) { $message = "Invalid UserEmail"; }

}

/* Validation to check if gender is selected */ if(!isset($message)) { if(!isset($_POST["gender"])) { $message = " Gender field is required"; } }

if(!isset($message)) { require_once("dbcontroller.php"); $db_handle = new DBController();

$query = "INSERT INTO users (username, name, last_name, gender, BirthMonth, BirthDay, BirthYear, Country, email, password, phone) VALUES ('" . $_POST["userName"] . "', '" . $_POST["name"] . "', '" . $_POST["lastName"] . "', '" .$_POST["gender"] . "', '" . $_POST["BirthMonth"] . "', '" . $_POST["BirthDay"] . "' , '" . $_POST["BirthYear"] ."','". $_POST["Country"] ."', '" . $_POST["userEmail"]. "','" . $_POST["password"]. "','".$_POST["Phone"]. "')"; $result = $db_handle->insertQuery($query);

编辑:格式化代码visib; e错误更好。 预先感谢任何回答的人。


你想检查你的电子邮件是否在数据库中退出,然后使用这段代码。 在这一行之后添加此代码。

/* Email Validation */ if(!isset($message)) { if(!filter_var($_POST["userEmail"], FILTER_VALIDATE_EMAIL))
 { $message = "Invalid UserEmail"; }

  }

你的数据库连接需要连接,所以首先连接这个

require_once("dbcontroller.php"); $db_handle = new DBController();

希望你的连接是好的,所以这段代码会检查

<?php

$sql = "SELECT anyfiled FROM yourtable WHERE email = " .$_POST['userEmail'];
$select = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($select);

 if (mysqli_num_rows($select) > 0) {
 $message = "exist";
 }
 ?>

电子邮件验证:除了检查常用符号(如“@”和字母数字)之外,还必须在开始时检查空格/制表符,并且最重要的是使用htmlspecialchars()将传入输入htmlspecialchars()为:

function reduceInput($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

调用$_POST['userEmail']上的reduceInput()函数,然后使用filter_var() reduceInput()函数对其进行验证。


我明白你的意思是不要将利用字符串传递给数据库? 您不必为此验证这些值。 有一个PHP函数可以将字符串安全地传递到数据库。 而已:

mysqli_real_escape_string($mysqli_object, $_POST['userName']);

但是,如果您想验证您的电子邮件和用户名,请执行以下操作:

if (ctype_alnum($nick)==false) exit(0);// this makes your nick can only contain letters and numbers
$emailB = filter_var($email, FILTER_SANITIZE_EMAIL);//this sanitizes your email
if ((filter_var($emailB, FILTER_VALIDATE_EMAIL)==false) || ($emailB!=$email)) exit(0);//this validates your email
链接地址: http://www.djcxy.com/p/69841.html

上一篇: how do i validate if my email is already exist?

下一篇: post data from form that was hidden using jquery