检查用户名的可用性时遇到问题

时间:2015-08-12 05:53:54

标签: php

我无法根据我的数据库检查用户名的可用性

没有错误,但它始终显示"用户名可用于注册"即使已经使用我输入的用户名注册了帐户。任何帮助/协助将不胜感激。

这是代码:

<html>
<head>
<title>check username</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script src="jquery.js" type="text/javascript" language="javascript">    </script>
<script language="javascript">
$(document).ready(function()
{
    $("#username").blur(function()
    {
        //remove all the class add the messagebox classes and start fading
    $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");

    //check the username exists or not from ajax
    $.post("user_availability.php",{ user_name:$(this).val() } ,function(data)
    {
      if(data=='no') //if username not avaiable
    //  if (data>0) //if username not avaiable
      {
        $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
        { 
          //add message and change the class of the box and start fading
          $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
        });     
      }
      else
      {
        $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
        { 
          //add message and change the class of the box and start fading
          $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);    
        });
      }

    });

});
});
</script>
<style type="text/css">
body {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:11px;
}
.top {
margin-bottom: 15px;
}
.messagebox{
position:absolute;
width:100px;
margin-left:30px;
border:1px solid #c93;
background:#ffc;
padding:3px;
}
.messageboxok{
position:absolute;
width:auto;
margin-left:30px;
border:1px solid #349534;
background:#C9FFCA;
padding:3px;
font-weight:bold;
color:#008000;

}
.messageboxerror{
position:absolute;
width:auto;
margin-left:30px;
border:1px solid #CC0000;
background:#F7CBCA;
padding:3px;
font-weight:bold;
color:#CC0000;
}

</style>
</head>
<body>


<br>
<br>
<div align="center">
<div class="top" >
</div>
<div >
   User Name : <input name="username" type="text" id="username" value=""/>
   <span id="msgbox" style="display:none"></span>
</div>

</div>
</body>
</html>

这就是useravailability.php:

<?php
include 'config.php';
$stmt = dbConnect()->$query("SELECT CUSERNAME from cust_account");
$unames = $stmt->fetchAll(PDO::FETCH_ASSOC);
$existing_users = array($unames);
//value got from the get metho
$username=$_POST['username'];
//checking weather user exists or not in $existing_users array
if (in_array($username, $existing_users))
{
//user name is not availble
echo "no";
} 
else
{
//user name is available
echo "yes";
}

?>

config.php是:

<?php
function dbConnect(){
   $username = "root";
   $password = "";
   $db_name="olep";
   $host="localhost";
    try{            
        $con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
        $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $con;
        }   catch(PDOException $e){
        echo "ERROR: ". $e->getMessage();
                                  }
}
?>

1 个答案:

答案 0 :(得分:0)

您的PHP代码可以通过以下方式更改:

include('config.php');

$stmt = dbConnect()->prepare('SELECT COUNT(CUSERNAME) FROM cust_account WHERE CUSERNAME = :username');
$stmt->bindParam(':username', $_POST['user_name'], PDO::PARAM_STR, 20);
$stmt->execute();

echo $stmt->fetchColumn() === 0 ? 'yes' : 'no';

将您的JS更改为:

$("#username").blur(function() {
  $username = $(this);
  //remove all the class add the messagebox classes and start fading
  $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");

  //check the username exists or not from ajax
  $.post("user_availability.php",{ user_name: $username.val().trim() } ,function(data) {
    if(data=='no') { // if username not avaiable
      $("#msgbox").fadeTo(200,0.1,function() { // start fading the messagebox

        //add message and change the class of the box and start fading
        $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900, 1);
      });     
    } else {
      $("#msgbox").fadeTo(200,0.1,function() { // start fading the messagebox

        // add message and change the class of the box and start fading
        $(this).html('Username available to register').addClass('messageboxok').fadeTo(900, 1);    
      });
    }
  });
});