检查用户名可用性时遇到麻烦


Trouble in checking availability of usernames

我在检查基于我的数据库的用户名的可用性有麻烦

没有错误,但它总是显示"用户名可用来注册",即使已经有一个注册帐户与我输入的用户名。如有任何帮助,我将不胜感激。

这是代码:

<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();
                                  }
}
?>

你的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);    
      });
    }
  });
});