-1

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

<?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>";
}
?>

这真的让我过去五天的工作感到窒息,任何帮助完成这项工作的人都将不胜感激。先感谢您。

4

2 回答 2

-2

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

使用下面获取记录返回的编号

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

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

即使这也无济于事,请对您的代码和逻辑进行故障排除。

于 2013-10-30T06:55:36.650 回答
-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";
}
于 2013-10-30T05:24:29.047 回答