2011-03-27 34 views
0
<?php 

if(isset($_POST[user_name])) 
{ 
$user_name=$_POST[user_name]; 
include("include/conn.php"); 
$sql_check = mysql_query("select userid from `vector`.`signup` where userid='".$user_name."'") 
or die(mysql_error()); 

//checking weather user exists or not in $existing_users array 
if (mysql_num_rows($sql_check)) 
{ //user name is not availble 
    echo "no"; 
} 
else 
{ 
    //user name is available 
    echo "yes"; 
} 
} 
?> 

jQuery代碼用戶名可用的或者是不使用PHP的AJAX

<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 
      { 
      $("#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> 

我「米做AJAX驗證用戶名存在與否但是這個代碼總是顯示可用即使用戶名存在於數據庫中。

使用這段代碼,我發送了沒有和是的jquery如果沒有用戶名不可用是然後可用

代碼有什麼問題??

+0

你能後的JavaScript的呢? – 2011-03-27 05:07:32

+0

定義了$ username嗎?您正在設置'$ user_name'的值。 – 2011-03-27 05:08:32

+0

您應該引用數組的鍵:'$ _POST ['user_name']',更重要的是,您應該採取預防措施來防止SQL注入攻擊。例如,如果'$ user_name'將包含'1'; DROP TABLE註冊; - 你會失去你的桌子。 – 2011-03-27 05:22:09

回答

1

vector中選擇用戶標識。 signup其中userid ='」。$用戶名。 「'」

  1. 我認爲你正在檢查用戶名,但使用的是userid

  2. 而且你已經$user_name=$_POST[user_name];,但使用的是$username

UPDATE

構建了$sql聲明。

$sql = "select userid from `vector`.`signup` where userid='".$user_name."'"; 

然後die($sql);

您將能夠在控制檯上獲取此查詢作爲響應。首先從mysql-console或phpmyadmin運行此查詢,並確保您獲得結果。

+0

userid是我在數據庫中的列名 – 2011-03-27 05:09:41

+0

@mr_eclair'userid'是否持有'usernames'? – 2011-03-27 05:12:02

+0

我做的更改仍然不起作用 – 2011-03-27 05:12:06

0
 <?php 

     if(isset($_POST[user_name])) 
     { 
     $user_name=$_POST[user_name]; 
     include("include/conn.php"); 
     $sql_check = mysql_query("select userid from `vector`.`signup` where userid='".$user_name."'") 
     or die(mysql_error()); 

     //checking weather user exists or not in $existing_users array 
     if (mysql_num_rows($sql_check)) 
     { //user name is not availble 
      echo 0; 
     } 
     else 
     { 
      //user name is available 
      echo "yes"; 
     } 
     } 
     ?> 
+1

爲什麼發佈2個答案?請結合他們。 – 2011-09-30 14:42:11

-1
 <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(isNaN(data)) //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>