日期:2014-05-16 浏览次数:20968 次
<form name="add" method="post" action="test.php"> 用户名:<input type="text" id="uname" name="username" /> </form>
$('#cname').blur(function(){
		if($('#uname').val() == ''){
			$('#result').html("<span style='color:red;'>不能为空</span>");
		}
		else{
			//var str = 'cname='+$('#cname').val();
			$.get('test.php',{username:$('#uname').val()},function(data){
				$('#result').html("<span style='color:red;'>"+data+"</span>");
			});
		}
	});
<?php 
mysql_connect('localhost','root','');
mysql_select_db('news');
mysql_query('set names utf8');
$a = $_GET['cname'];
$sql = "select * from tnewscategory where cCategoryName = '$a'";
// $sql = "select count(*) from tnewscategory where cCategoryName = '$a'";
/*
问题就出在这个地方了,我原来用的是count(*)的查询语句查询的,但是验证的结果都是可以注册,不管是数据库里已存在的,还是未存在的数据,都提示可以注册,后来换成 select * 就正常了。这两种查询方法,最终的查询结果都是一个资源类型的。最终使用mysql_num_rows()转化后,也确实得了一个数值,却不知道为何返回到ajax验证的时候,却无法成功。当初使用count(*);  想着这种效率更快些,不想却出了这样的情况,暂时记录下吧,以后找到原因,再做更新
*/
$n = mysql_num_rows(mysql_query($sql));
if($n > 0){
   echo "已存在";
   exit;
}
else{
    echo "可以注册";
    exit;
}
?>