简体   繁体   English

PHP / MySql用户注册,我如何使要注册的用户不使用相同的用户名?

[英]PHP/MySql user registration, how can i make a user that wants to register not use the same username?

I'm making a web page, and I want the users to have a username and a password, but I don't want the usernames to repeated, My database is already connected to my web page and a section for them to put their information, such as name, username, password, etc. 我正在制作一个网页,我希望用户有一个用户名和密码,但我不想重复这些用户名,我的数据库已经连接到我的网页,并有一个供他们放置其信息的区域,例如名称,用户名,密码等。

I need help on how to warn the user to input another username if the one they have entered is already taken. 如果已使用他们输入的用户名,我需要如何警告用户输入其他用户名的帮助。 Here's my code in HTML/PHP/MySQL: 这是我在HTML / PHP / MySQL中的代码:

<!DOCTYPE html>
<html>
<head>
  <title>Skillfair</title>
  <meta charset="utf-8"/>
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <link rel="stylesheet" type="text/css" href="stylesbd_o.css">
</head>
<body>
  <section id="header" class="jumbotron">
  <header class="container">
    <div class="row">
      <h2 class="col-sm-4">Database</h2>
      <nav class="col-sm-8 text-right">
          <p id="btnregister">Register</p>
          <!--antes estaba escrito newest en vez de register-->
        <p id="btncatalogo">Catalogue</p>
        <p id="btnlogin">Login</p>
          <!--antes estaba escrito contact en vez de login-->
          <p id="btncart">Cart</p>
      </nav> 
    </div>
  </header>
  </section>

     <!--Seccion de la parte del registro -->
    <section id="registro1"  class="container">
    <div id="registro">
    <div class="row">
    <figure class="col-sm-12">
    <form id="form1" name="form1" method="post" action="" onsubmit="return valido()">
            <fieldset class="col-sm-12">
           <h1>Register <span> now!</span></h1>
            <input name="username" type="text" placeholder="You must input your username" maxlength="20">    
            <input name="password" type="password" placeholder="You must input your password" maxlength="20">
            <input name="name" type="text" placeholder="You must input your name(s)" maxlength="20">                
            <input name="lastname" type="text" placeholder="You must input your lastname" maxlength="20">
            <input name="school" type="text" placeholder="You must input your school" maxlength="20">
            <input name="age" type="number" placeholder="You must input your age" maxlength="2">
            <input name="phone" type="tel" placeholder="You must input your phone number" maxlength="20">
            <input name="email" type="email" placeholder="You must input your email" maxlength="35">
            <button type="submit">Sent Data</button>
            <input id="reset" type="reset" value="Clear fields" id="reset">
            </fieldset>
        </form>
        <p id="sent">
        </figure>
    </div>
  </div>
  </section>

    <!--Seccion de la parte del catalogo -->
  <section  class="container">
    <div id="catalogo">
    <div class="row">
       <figure class="col-sm-6">
         <p>-kitchen</p>
         <img src="https://s3.amazonaws.com/codecademy-content/projects/make-a-website/lesson-4/kitchen.jpg"/>
       </figure>
       <figure class="col-sm-6">
         <p>-woodwork</p>
         <img src="https://s3.amazonaws.com/codecademy-content/projects/make-a-website/lesson-4/woodwork.jpg"/>
       </figure>     
    </div>
    <div class="row">
       <figure class="col-sm-6">
         <p>-gifts</p>
         <img src="https://s3.amazonaws.com/codecademy-content/projects/make-a-website/lesson-4/gifts.jpg"/>
       </figure>
       <figure class="col-sm-6">
         <p>-antiques</p>
         <img src="https://s3.amazonaws.com/codecademy-content/projects/make-a-website/lesson-4/antique.jpg"/>
       </figure>      
    </div>
  </div>
  </section>

  <footer class="container">
    <div class ="row">
      <p class="col-sm-4">&copy; 2016 Skillfair</p>
    </div>
  </footer>

    <!-- Faltan registros (javascript)-->
    <script type="text/javascript">
            function valido () {
                if(document.form1.username.value=="") {
                    alert ('You must input your username');
                    document.form1.username.focus();
                    return false;
                }   
                if(document.form1.password.value=="") {
                    alert ('You must input your password');
                    document.form1.password.focus();
                    return false;
                }   
                if(document.form1.name.value=="") {
                    alert ('You must input your name');
                    document.form1.name.focus();
                    return false;
                }   
                if(document.form1.lastname.value=="") {
                    alert ('You must input your last name');
                    document.form1.lastname.focus();
                    return false;
                }   
                if(document.form1.school.value=="") {
                    alert ('You must input your school');
                    document.form1.school.focus();
                    return false;
                }
                if(document.form1.age.value=="") {
                    alert ('You must input your age');
                    document.form1.age.focus();
                    return false;
                }
                if(document.form1.phone.value=="") {
                    alert ('You must input your phone number');
                    document.form1.phone.focus();
                    return false;
                }
                if(document.form1.email.value=="") {
                    alert ('You must input your email');
                    document.form1.email.focus();
                    return false;
                }
                    else {
                    alert ('Success!!');
                    return true;
                }
            }

            </script>

<script>
    /* script para la apertura y cierre del boton register */
$(document).ready(function(){
    $("#btnregister").click(function(){
       $("#registro").fadeToggle("slow");
       $("#catalogo").hide();
    });
    $("#registro").hide();

});
    /* script para la apertura y cierre del boton catalogo */
$(document).ready(function(){
    $("#btncatalogo").click(function(){
       $("#catalogo").fadeToggle("slow");
       $("#registro").hide();
    });
    $("#catalogo").hide();

});

</script>

    <?php
    //Connect to database
    mysql_pconnect('localhost:8889','xxxx','xxxx');
    //***********************
    //Select database
    mysql_select_db('users');
    //***********************
    //Insert data into database
    if (isset($_POST['username']))
    {
    $username=$_POST['username'];    
    $password=$_POST['password'];
    $name=$_POST['name'];
    $lastname=$_POST['lastname'];
    $school=$_POST['school'];
    $age=$_POST['age'];
    $phone=$_POST['phone'];
    $email=$_POST['email'];
    $cad="insert into users.data (username, password, name, lastname, school, age, phone, email) values ('".$username."','".$password."','".$name."','".$lastname."','".$school."','".$age."','".$phone."','".$email."') ";
    mysql_query($cad);
    }
    ?>


</body>
</html>

I'm sorry if the comments and some other things are in spanish because my code was originally in spanish but i tried to translate it as much as i could, so please, if anybody could help me with this issue that would be great!! 很抱歉,注释和其他内容是西班牙语的,因为我的代码最初是西班牙语的,但是我尝试了尽可能多的翻译,因此,如果有人可以帮助我解决这个问题,那就太好了!!

You can achieve the functionality using ajax. 您可以使用ajax实现该功能。 Just post data to the php file which checks for duplication using ajax. 只需将数据发布到php文件即可,该文件使用ajax检查是否重复。

Here is an example which should solve your problem. 这是应该解决您的问题的示例。

HTML 的HTML

<input type="text" id="user" name="user" disabled="true" required><br><span id="usererr"></span>

jquery code jQuery代码

$("#user").keyup(function (e) {
    e.preventDefault();
    var user = $(this).val();
    var dataString = 'user=' + user;

    $.ajax({
        type: 'POST',
        url: 'checkusername.php',
        data: dataString,
        cache: false,
        success: function (html)
        {

            $("#usererr").html(html);

        }
    });
 });

PHP (CODE checkuser.php) PHP(CODE checkuser.php)

if($_POST)
{
 if(isset($_POST['user']))
 {
    $user= strtolower(sanitizeString($_POST['user']));

    $query="select * from users.data where username='".$user."'";
    $result=  mysql_query($query);
    if(mysql_num_rows($result)==0)
    {
        echo '<font color="green">Username is Available</font><br>';
    }
    else
    {
        echo '<font color="red">Username is already taken. Please select another username.</font><br>';
        echo '<script language="javascript">var user=document.getElementById("user");'
        . 'user.value="";</script>';
    }

 }
}

NOTE: mysql_* functions are depreciated and no longer used. 注意:mysql_ *函数已弃用,不再使用。 I have provided the code as per your need. 我已根据您的需要提供了代码。 It is highly recommended that you upgrade to mysqli_* functions. 强烈建议您升级到mysqli_ *函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用户名也出现在另一个用户处? - How can I make the Username also appear at another User? 如何获取用户要保存的列表项的标题 - How can I get the title of the list item the user wants to save 如果用户名在同一页面上,则使用AJAX和PHP来“警告”用户? - Use AJAX and PHP to “Alert” the user if the username exists on the same page? 如何确保每个用户的注册表格都是唯一的,而不是相同的用户名,电子邮件或地址等? - How to ensure registration form is unique for each user rather than the same username or email or address etc? 如何重定向到instagram:// user?username = {username} - How to make redirect to instagram://user?username={username} 我需要创建一个表,以便用户可以填写他想要的内容 - I need to create a table that the user can fill in with what he wants 如果他真的想离开页面,我如何要求网络用户确认? - How can I ask a web user for confirmation if he really wants to leave the page? 如何检测用户的浏览器和操作系统,并使用它来动态修改PHP页面中的MySQL数据库查询 - How can I detect browser and OS of user and use this to dynamically modify my MySQL database query from a PHP page 用户注册和认证(注册和登录)Node.js - User registration and authentication (register and login) Nodejs Sinch在php中注册用户 - Sinch Register User in php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM