如何避免在php表单中多次输入相同的用户名?

时间:2013-12-27 16:31:22

标签: php mysql

我对此感到震惊!我需要避免多次输入相同的用户名..如果发生这种情况也要通知错误的密码..这里是代码:我是新手所以帮助我..

<?php 
include('include/header.php');
if(isset($_POST['donor']) || $_POST['donor']!=""){
    $msg    =   $productFetch->addproduct($_REQUEST);
}
if(isset($_POST['reghos']) || $_POST['reghos']!=""){
    $msg    =   $recepientFetch->addrecepient($_REQUEST);
    $msg    =   "Hospital Registered Successfully";
}
    if(isset($_POST['hospitallogin'])){
    $username   =   $_POST['name'];
    $password   =   md5($_POST['password']);
    $qry        =   "select * from `hospitaluser` where `firstname`='$username' and      hospitaluserpassword='$password' and `hospitaluserlevel`='1'";

    if(mysql_num_rows(mysql_query($qry))>0){
        $hospital_user  =   mysql_fetch_object(mysql_query($qry));
        $_SESSION['hospital_user']=$hospital_user->firstname;
        $_SESSION['hospital_user_id']=$hospital_user->id;
        $msg    =   "Successfully Login";
    }
}
$qry="";
if(isset($_POST['hospitalregister'])){
    $firstname  =   $_POST['firstname'];
    $password   =   md5($_POST['hospitaluserpassword']);
    $cpassword  =   md5($_POST['hospitalusercpassword']);
    if($password==$cpassword){
    $qry        =   "select * from `hospitaluser` where `firstname`='$firstname' and `hospitaluserlevel`='1'";

        if(mysql_num_rows(mysql_query($qry))<=0){
            $msg    =   $hospitaluserFetch->addhospitaluser($_REQUEST);
            $msg    =   "Successfully Registered";
        }else{
            $msg    =   "The User Name Is Alread Exist! Choose Another One!";
        }
    }else{
        $msg="Password Doesn't Match";
    }
}

1 个答案:

答案 0 :(得分:0)

在mysql中试试这个:

ALTER TABLE hospitaluser ADD UNIQUE (firstname);

这会在firstname列上创建一个唯一索引,从而阻止您插入两次相同的用户名。

如需更多帮助,您可以添加addhospitaluser的代码吗?