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

标签 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";
    }
}

最佳答案

在 mysql 中尝试一下:

ALTER TABLE hospitaluser ADD UNIQUE (firstname);

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

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

关于php - 如何避免在 php 表单中多次输入相同的用户名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20804161/

相关文章:

php - FatalThrowableError 调用未定义的函数 App\Console\Commands\getCNofDNn()

php - 如何在亚马逊网络服务器中迁移已部署的 laravel 项目

java - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : Unknown column 'quantity' in 'field list'

mysql - 选择最后一个事件类型为 X 的所有用户

mysql - Mysql Provider 的 Ado.net Entity Framework 中的多个数据库

mysql - 无法更新 MySQL 现有记录

php - 如何限制/授权对 PHP 脚本的访问?

php - 从表单中获取用户名、密码和电子邮件,并用它创建数据库行

PHP/HTML - 没有登录但来自导航链接的个人资料页面

mysql - 回滚失败的 Rails 迁移