我的注册表中忽略了验证


Verification ignored in my registration form.

我正在尝试验证注册表中的字段(密码匹配)用户ID未被占用以及电子邮件。

这是我的代码

<?php
    if(isset($_POST['submit'])){
        $msg="";
        $id="";
        $name=$_POST['cName'];
        $address=$_POST['cAddrss'];
        $country=$_POST['cCountry'];
        $mobil=$_POST['cTel'];
        $sex=$_POST['cSex'];
        $email=$_POST['cEmail'];
        $userName=$_POST['cUsername'];
        $userPassword=$_POST['cPassword'];
        $confPassword=$_POST['concPassword'];
        $checkEmail=$db->query("select * from users where email ='".$email."'");
        $checkUID=$db->query("select * from users where user_name ='".$userName."'");
        if($userPassword !== $confPassword)
        {
            $msg="Password don't match";
        }
        if($checkEmail -> num_rows == 1)
        {
            $msg = "<span style='color:#F00; font-size:14px; font-weight:bold;'>This email already taken</span>";
        }
        if($checkUID -> num_rows == 1) 
        {
            $msg = "<span style='color:#F00; font-size:14px; font-weight:bold;'>This User name already taken</span>";
        }
        if($put=$db->prepare("INSERT INTO users(id, name, mobile, sex, country, address, user_name, user_password, email)VALUE(?, ?, ?, ?, ?, ?, ?, ?, ?)"))
            {
                $put->bind_param('issssssss', $id, $name, $mobil, $sex, $country, $address, $userName, $userPassword, $email);
                $put->execute();
                $put->close();
            }
            else
            {
                die($db->error);
            }
            header("Location:index.php?pid=3&smsg=smsg");
        }
?>

发生的情况是表单忽略我的验证并跳转到标题并将我发送到成功页面,因为一切都很好。

在我的表格顶部,我用它来显示$msg是否有任何<?php if(!empty($msg)) {echo $msg;} ?>

试试这种方法:

if($userPassword !== $confPassword){
    $msg.="Password don't match";
}else{
    //Password matches, now we can hit the database
    $checkEmail=$db->query("select * from users where email ='".$email."'");
    $checkUID=$db->query("select * from users where user_name ='".$userName."'");
    $emailExist =($checkEmail -> num_rows > 0);
    $userNameExist = ($checkUID -> num_rows > 0);
    if(!$emailExist && !$userNameExist){
        $put=$db->prepare("INSERT INTO users(id, name, mobile, sex, country, address, user_name, user_password, email)VALUE(?, ?, ?, ?, ?, ?, ?, ?, ?)");
        $put->bind_param('issssssss', $id, $name, $mobil, $sex, $country, $address, $userName, $userPassword, $email);
        if($put->execute()){        
          //record Inserted now redirect
          $msg .= "Success! no validation error";
          header("Location:index.php?pid=3&smsg=smsg");
        }else{
         //error
          $msg = "error while INSERT";
          die($db->error);
        }
    }else{
        //found record  
        $msg.= $emailExist ? "This email already taken" : "";
        $msg.= $userNameExist ? "This User name already taken" : "";
    }
}
echo $msg;

  • 仅在密码匹配时调用数据库
  • 新增成功条件
  • 将重定向代码移至成功块
  • 错误消息的串联