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

Php 如何验证我的电子邮件是否已存在?,php,Php,编辑:将代码格式化为visib;更好地处理错误。提前感谢所有回答的人。电子邮件验证: 除了检查诸如“@”和字母数字之类的常用符号外,还必须检查“开始”中的空格/制表符,最重要的是使用htmlspecialchars()将传入的输入转换为: if(count($_POST)>0) { /* Form Required Field Validation / foreach($_POST as $key=>$value) { if(empty($_POST[$key])) { $mes

编辑:将代码格式化为visib;更好地处理错误。提前感谢所有回答的人。

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

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);

调用
$\u POST['userEmail']
上的
reduceInput()
函数,然后使用您的
filter\u var()
函数对其进行验证。

我理解您的意思是不将利用字符串传递到数据库?您不必为此验证这些值。有一个php函数可以将字符串安全地传递到数据库。就是这样:

function reduceInput($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
但如果您想验证您的电子邮件和用户名,请执行以下操作:

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
您的数据库连接需要为此进行连接,因此请先连接此数据库

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

  }
希望您的连接正常,以便此代码将进行检查

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


请将您的代码格式化为code对不起..当我输入代码时,它一直在说错误..这是我第一次向您提问stackoverflow@chanterrainey你可能需要在文章中添加更多的文本,而不仅仅是代码,这就是为什么你会从SO那里得到错误。你甚至没有为此使用谷歌。关于如何做这件事,有很多例子可供参考1.对不起,这个问题可能是重复的。。我的意思是,我如何添加一个条件,告诉我输入的电子邮件是否已经存在于数据库中OK。我现在发布了正确的答案…那么我将把这个代码放在哪里呢?我应该为此创建一个新的php文件吗?$sql=“从您的表中选择任意字段,其中email=“.$\u POST['userEmail'”$select=mysqli\u查询($con,$sql)$row=mysqli\u fetch\u assoc($select);如果(mysqli_num_rows>0){message=“exist”}我将此代码放在电子邮件验证行之后。现在它在第35行的C:\xampp\htdocs\final\register.php中显示意外的“=”,请立即检查
<?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";
 }
 ?>