如果我完全填写了我的表格,即使我的数据库中已经有它,电子邮件可用性检查也会失败,但是如果我将任何字段留空或输入与所需内容相反的内容,则会报告一个错误,并且还会报告电子邮件可用性的错误该电子邮件已被使用。这严重阻碍了我在项目中取得进展。
<?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>";
}
?>
这真的让我过去五天的工作感到窒息,任何帮助完成这项工作的人都将不胜感激。先感谢您。