用户名可用性检查不适用于PHP

时间:2013-10-30 03:08:46

标签: php mysql pdo

如果我完全填写表单,即使我已经在我的数据库上有电子邮件可用性检查失败但是如果我将任何字段留空或输入与所需内容相反的内容,则会报告错误,并且将报告错误另据报道,该电子邮件已被使用。这严重阻碍了我在项目中取得进展。

<?PHP
   if(isset($_POST['submit'])) 
{
  if (empty($_POST["firstName"]))
   {$Err[] = "* First Name is required";}
  else
 {
  $name = test_input($_POST["firstName"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name))
  {
  $Err[] = "Only letters are allowed in First Name"; 
  }
}

if (empty($_POST["email"]))
  {$Err[] = "* Email is required";}
else
{
  $email = test_input($_POST["email"]);
// check if e-mail address syntax is valid
  if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
  {
    $Err[] = "Invalid email format"; 
  }
  else 
{
$emailSQL = "SELECT email FROM userdetails WHERE email = ?";
$SQ = $conn->prepare($emailSQL) or die("ERROR: " . implode(":", $conn->errorInfo()));

    $SQ->bindParam(1, $email);
    $SQ->execute();
    /* $result = $SQ->fetch(); */
    $count = $SQ->rowCount();
if($count !== 0){
    $Err[] = "Sorry, email is already in use by another user";
}}}

if (empty($_POST["surname"]))
        {$Err[] = "* Surname is required";}
else
    {
        $surname = test_input($_POST["surname"]);
        // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name))
   {
        $Err[] = "Only letters are allowed in Surname"; 
    }
   }
if (empty($_POST["password"]))
        {$Err[] = "* Password is required";}
else
   {
        $password = test_input($_POST["password"]);
}
if (empty($_POST["userName"]))
         {$Err[] = "* Username is required";}
else 
    {
      $username = test_input($_POST["userName"]);
     // check if name only contains letters and whitespace
      if (!preg_match("/^[a-zA-Z ]*$/",$name))
    {
       $Err[] = "Only letters are allowed in Username"; 
    }
   }

if (count($Err > 0)) {
    echo "<div id = 'errors'>";
    foreach ($Err as $error) {
        echo $error;
        echo "<br />";
    }
    echo "</div>";
}
?>

过去五天我的工作真的令人窒息,任何获得这项工作的帮助都将深受赞赏。提前谢谢你。

2 个答案:

答案 0 :(得分:-2)

替换

if($count !== 0){
   $Err[] = "Sorry, email is already in use by another user";
}

if($count > 0){
  $Err[] = "Sorry, email is already in use by another user";
}

答案 1 :(得分:-2)

rowCount()重新运行上次删除,插入或更新sql语句所影响的行数。

使用以下内容获取记录返回的数字

$row = $SQ->fetch(PDO::FETCH_NUM);

if($row[0] > 0){
    $Err[] = "Sorry, email is already in use by another user";
}

即使这样也无济于事,请对代码和逻辑进行排查。