人们注册时如何防止重复用户名? [英] How to prevent duplicate usernames when people register?

查看:29
本文介绍了人们注册时如何防止重复用户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在制作登录/注册系统,并且即将完成代码的注册部分.我遇到的唯一问题是如何使用户无法使用重复的用户名进行注册.我想让它工作,这样我的数据库就不会接受信息,它会告诉用户错误.

I have been making a login/register system and I am drawing close to finishing my register portion of code. The only problem I am running into is how to make it so that users cannot register with duplicated usernames. I want it to work so that my database won't accept the information, and it will tell the user about the error.

我的 PHP

<?php

include 'database_connection.php';
if (isset($_POST['formsubmitted'])) {
    $error = array(); //Declare An Array to store any error message
if (empty($_POST['name'])) {//if no name has been supplied
    $error[] = 'Please Enter a name '; //add to array "error"
} else {
    $name = $_POST['name']; //else assign it a variable
}

    if (empty($_POST['e-mail'])) {
        $error[] = 'Please Enter your Email ';
    } else {
        if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['e-mail'])) {
            //regular expression for email validation
            $Email = $_POST['e-mail'];
        } else {
            $error[] = 'Your EMail Address is invalid  ';
        }
    }

    if (empty($_POST['Password'])) {
        $error[] = 'Please Enter Your Password ';
    } else {
        $Password = $_POST['Password'];
    }

    if (empty($error)) {
        //send to Database if there's no error '
    }
}

推荐答案

例如,当用户发布用户名并单击提交时,您可以这样做,您可以编写此代码或通过修改将其添加到您的代码中:

You can do it like this when the user post the username for example and click submit you can write this code or add it to your code with your modification :

<?php

$connect = mysql_connect('whatever', 'whatever', 'whatever');
$database = mysql_select_db('your dbname');
$username = $_POST['username'];
if (isset($username)) {
    $mysql_get_users = mysql_query("SELECT * FROM table_name where username='$username'");

    $get_rows = mysql_affected_rows($connect);

    if ($get_rows >= 1) {
        echo "user exists";
        die();
    } else {
        echo "user do not exists";
    }
}

这篇关于人们注册时如何防止重复用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆