如何在当前页面上显示PHP Duplicate Entry错误消息

时间:2015-11-29 18:54:21

标签: php html mysql forms

我正在为我正在开发的网站创建一个注册表单。我正在使用PHP将表单插入到我的数据库中。我将useremail设置为我的数据库中的唯一键。当我输入已存在于我的数据库中的电子邮件时,它会带我到一个带有此

的空白页面
  

错误消息:重复输入''关键字' email_2'

如何在不清除表单的情况下将此消息显示在表单上方?在代码中" SERVER ERROR"并且"两个领域必须完整"文本显示在正确的位置。如何以相同的方式显示重复输入错误?

这是我目前拥有的PHP代码。请注意我已成功连接到我的数据库,但此处未包含代码。

$firstName  = $_POST['firstname'];
$lastName  = $_POST['lastname'];
$useremail  = $_POST['useremail'];
$password  = $_POST['password'];
$gender  = $_POST['gender'];
$birthday  = $_POST['birthday'];
$sqlDate = date('Y-m-d');

if (     $firstName!=''&&$lastName!=''&&$useremail!=''&&$password!=''&&$gender!=''&&$birthday!='')
{
   $sql = "INSERT INTO user (fname, lname, email, password, gender, birthday) VALUES ('$firstName', '$lastName', '$useremail', '$password', '$gender', '$birthday')";

   $res = mysql_query($sql) or die(mysql_error());
   if($res)
   {  

 header('location: http://myticapp.com/thankyou.php?useremail='.$useremail);
   }
   else
   {

 echo"<center><div class=\"denied\">SERVER ERROR</div></center>";
   }

}
else if(!empty($_POST))
{
  echo"<center><div class=\"denied\">BOTH FIELDS MUST BE COMPLETE</div>    </center>";
}

以下是表格代码:

  `<form action="#" method="POST" class="signupform">
                        <input type="text" name="firstname" placeholder="First Name" required/>
                        <br>
                        <input type="text" name="lastname" placeholder="Last Name" required/>
                        <br>
                        <input type="text" name="useremail" placeholder="Email" required/>
                        <br>
                        <input type="text" name="password" placeholder="Password" required/>
                        <br>
                        <input type="text" name="gender" placeholder="Gender" required/>
                        <br>
                        <input type="text" name="birthday" placeholder="Birthday (YYYY-MM-DD)" required/>
                        <br>
                        <input type="submit" class="button buttonhover"/>
                    </form>`

3 个答案:

答案 0 :(得分:1)

您必须在插入数据之前运行检查查询。

<?
$firstName  = $_POST['firstname'];
$lastName  = $_POST['lastname'];
$useremail  = $_POST['useremail'];
$password  = $_POST['password'];
$gender  = $_POST['gender'];
$birthday  = $_POST['birthday'];
$sqlDate = date('Y-m-d');

$useremail = mysql_escape_string($useremail);
$duplicate = false;

try {
    $sql = "SELECT COUNT(*) as count FROM user WHERE email = '$useremail'";
    $res = mysql_query($sql)  or die(mysql_error());
    $data = mysql_fetch_assoc($res);
    if ($data['count'] > 0) {
        echo '<center>EMAIL ALREADY IN USE</center>';
        $duplicate = true;
    }
} catch (Exception $e) {

}

if (     $firstName!=''&&$lastName!=''&&$useremail!=''&&$password!=''&&$gender!=''&&$birthday!=''&&$duplicate==false)
{

   try
   {
        $sql = "INSERT INTO user (fname, lname, email, password, gender, birthday) VALUES ('$firstName', '$lastName', '$useremail', '$password', '$gender', '$birthday')";
        $res = mysql_query($sql)  or die(mysql_error());
    } 
    catch (Exception $e) 
    {
       echo "<center><div class=\"denied\">",  $e->getMessage(), "</div></center>";

    }


  if(isset($res))
   {  
      header('location: http://myticapp.com/thankyou.php?useremail='.$useremail);
   }
   else
   {

 echo"<center><div class=\"denied\">SERVER ERROR</div></center>";
   }

}
else if(!empty($_POST))
{
  echo"<center><div class=\"denied\">BOTH FIELDS MUST BE COMPLETE</div>    </center>";
}

答案 1 :(得分:0)

除了针对句柄案例的@ EchoUA.COM答案,您尝试插入已有的电子邮件,您也可以尝试这样的事情:

$firstName  = $_POST['firstname'];
$lastName  = $_POST['lastname'];
$useremail  = $_POST['useremail'];
$password  = $_POST['password'];
$gender  = $_POST['gender'];
$birthday  = $_POST['birthday'];
$sqlDate = date('Y-m-d');

if (     $firstName!=''&&$lastName!=''&&$useremail!=''&&$password!=''&&$gender!=''&&$birthday!='')
{

   try
   {
        $sql = "INSERT INTO user (fname, lname, email, password, gender, birthday) VALUES ('$firstName', '$lastName', '$useremail', '$password', '$gender', '$birthday')";
        $res = mysql_query($sql)  or die(mysql_error());
    } 
    catch (Exception $e) 
    {
       echo "<center><div class=\"denied\">",  $e->getMessage(), "</div></center>";

    }


  if(isset($res))
   {  
      header('location: http://myticapp.com/thankyou.php?useremail='.$useremail);
   }
   else
   {

 echo"<center><div class=\"denied\">SERVER ERROR</div></center>";
   }

}
else if(!empty($_POST))
{
  echo"<center><div class=\"denied\">BOTH FIELDS MUST BE COMPLETE</div>    </center>";
}

基本上你尝试执行insert查询,如果不成功,你将捕获错误,回显它,然后你的代码将被执行。

答案 2 :(得分:-1)

    $email                 = ($_POST['email']);   
    $email_duplicate_error = ("SELECT email FROM users WHERE '{$email}' == 
    email");
    if($email_duplicate_error){
     //Your message .. .
     $previous = "<script>window.history.go(-1);</script>";
     echo $previous;

}