用户名验证不起作用

时间:2014-06-26 08:26:57

标签: php mysql validation username

我想验证用户名是否在我的数据库中,但不能正常工作。我在我的数据库上添加用户名,但是当我验证它的show username可用时,现在该做什么。我把我的代码

多数民众赞成Dbconnector.php

<?php

class DbConnector {

    var $theQuery;
    var $link;

    function DbConnector(){

            // Get the main settings from the array we just loaded
            $host = 'host';
            $db = 'cms';
            $user = 'root';
            $pass = '';

            // Connect to the database
            $this->link = mysql_connect($host, $user, $pass);
            mysql_select_db($db);
            register_shutdown_function(array(&$this, 'close'));

        }

      //*** Function: query, Purpose: Execute a database query ***
        function query($query) {

            $this->theQuery = $query;
            return mysql_query($query, $this->link);

        }

        //*** Function: fetchArray, Purpose: Get array of query results ***
        function fetchArray($result) {

            return mysql_fetch_array($result);

        }

        //*** Function: close, Purpose: Close the connection ***
        function close() {

            mysql_close($this->link);

        }

    }

    ?>

那就是Check.php

<?php
include("dbConnector.php");
$connector = new DbConnector();

$username = trim(strtolower($_POST['username']));
$username = mysql_escape_string($username);

$query = "SELECT Username FROM admin WHERE Username = '$username' LIMIT 1";
$result = $connector->query($query);
$num = mysql_num_rows($result);

echo $num;
mysql_close();

?>

那就是我的index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Papermashup.com | jQuery PHP Username Checker</title>
<link href="../style.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script>
$(document).ready(function(){
$('#username').keyup(username_check);
});

function username_check(){  
var username = $('#username').val();
if(username == "" || username.length < 4){
$('#username').css('border', '3px #CCC solid');
$('#tick').hide();
}else{

jQuery.ajax({
   type: "POST",
   url: "check.php",
   data: 'username='+ username,
   cache: false,
   success: function(response){
if(response == 1){
    $('#username').css('border', '3px #C33 solid'); 
    $('#tick').hide();
    $('#cross').fadeIn();
    }else{
    $('#username').css('border', '3px #090 solid');
    $('#cross').hide();
    $('#tick').fadeIn();
         }

}
});
}



}

</script>

<style>
#username{
    padding:3px;
    font-size:18px;
    border:3px #CCC solid;
}

#tick{display:none}
#cross{display:none}


</style>
</head>

<body>



Here are some usernames that have been put in the database:<br/><br />

Hammad, Huzaifa , Hanzlah<br/><br/>

Username: <input name="username" id="username" type="text" />
<img id="tick" src="tick.png" width="16" height="16"/>
<img id="cross" src="cross.png" width="16" height="16"/>


</body>
</html>

1 个答案:

答案 0 :(得分:0)

我认为问题出在你的HTML和Js上。

首先添加一个表单并为其添加一个ID,例如#load,然后将<input...../>放入表单中。

现在将您的Jquery.ajax({ url:更改为url +'check.php?'+$("#load").serialize()

尝试并确保使用Js控制台检查是否发送了ajax请求及其反馈。