为什么我的PHP验证没有检查数据库中的现有电子邮件地址?

时间:2014-11-06 17:50:30

标签: php validation email

我有一个基本的注册表单,其中包含所有输入和ph​​p验证的jaquery验证,以检查现有的电子邮件地址。但是,如果我输入相同的电子邮件地址并多次提交,我就不会收到任何验证消息,并且电子邮件会存储在数据库中。我无法理解为什么会发生这种情况,我认为我的代码中没有任何错误。

以下是PHP验证:

 public function register_post(){   
    $register = new Register();
    $register->loadFromPost();

    $valid_error = array();

    if($register->is_value_exist('email', $register->email)){
        $valid_error[] = 'email already taken';
    }

    $regexp = "/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/";

    if (!preg_match($regexp, $register->email)) {
        $valid_error[] = "not a valid email";
    } 

    $var_array = array($register->first_name, $register->last_name);
    $regexp = "/^[a-zA-Z]{2,16}+$/";
    foreach($var_array as $var){
        if (!preg_match($regexp, $var)) {
            $valid_error[] = "not a valid input value for first name or last name";
        }
    }
    if(count($valid_error) == 0)
    {
        $hash = Membership::hash($register->password);
        $register->password = $hash['password'];
        $register->hash = $hash['salt'];
    }
    $register->save();

    $redirect_url = SITE_URL . "home/index";
    redirect($redirect_url);
}

3 个答案:

答案 0 :(得分:1)

您的保存和重定向始终会被调用,因为它们不在您的IF条件下。改为:

if(count($valid_error) == 0)
{
    $hash = Membership::hash($register->password);
    $register->password = $hash['password'];
    $register->hash = $hash['salt'];

    $register->save();
    $redirect_url = SITE_URL . "home/index";
    redirect($redirect_url);
}

只有在没有验证错误时才会调用这三行。您还需要创建一种方法将valid_errors发送回用户,以便在他们不被重定向时,他们知道原因。

由于有人评论过它......至于电子邮件的正则表达式,请看一下:http://www.regular-expressions.info/email.html。你不允许+或%或。 - 等等。

答案 1 :(得分:0)

您可以像我的代码一样尝试使用完全jquery

    $(document).ready(function(){
//validation 
     $("form[name='your form name']").validate({ 
            rules: {
                uname:{
                    required:true,
                    alphabetsnspace:true,
                    minlength: 3,
               },
                email:{
                    required:true,
                    email:true,
                },

                }, 
                psw: { 
                    required: true,
                       minlength: 5,
                       maxlength: 25,

                  }, 
                  rpsw: { 
                    equalTo: "#psw",
                     minlength: 5,
                     maxlength: 25,
               }
            },
            messages: {
                uname:{ 
                    required:"Please enter your user name",
                    minlength:"User name should be more then 3 characters",
                    alphabetsnspace:"Only alphabets and numbers are allowd",
                      },
                email:"Please enter a valid email address",

         },
//calling php 
            submitHandler: function(form) {
                $.ajax({
                    url :"register.php",
                    type:"post",
                    dataType : 'html',
                    data:$('#registerform').serialize(),
                    success: function(data) 
                    {
                     //any function
                     }
                   });
    });
    });

答案 2 :(得分:0)

尝试此操作以进行多次插入验证

if(isset($email))
{
 $checkdata=" SELECT email FROM users WHERE email='$email' ";

 $query=mysqli_query($conn,$checkdata);

 if(mysqli_num_rows($query)>0)
 {
  echo "email already taken";
  exit;
 }
 else
 {
//inset
}