2012-03-19 10 views
1

iPhone代碼MySQL查詢語法錯誤在PHP中使用時

當我使用此代碼它始終顯示錯誤密碼,但我輸入正確的憑據。

NSString *post =[NSString stringWithFormat:@"UserName=%@&UserPas sword=%@",userNameTextField.text, userPasswordTextFiled.text]; 

    NSString *hostStr = @"http://www.celeritas-solutions.com/emrapp/connect.php"; 
    = [hostStr stringByAppendingString:post]; 
    NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];  
    NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding]; 
    if([serverOutput isEqualToString:@"Yes"]){ 
      UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Congrats" message:@"You are authorized " 
                  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
     [alertsuccess show]; 
      [alertsuccess release]; 


    } else { 
      UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Username or Password Incorrect" 
                  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
      [alertsuccess show]; 
      [alertsuccess release]; 

    } 

我得到驗證從數據庫中的用戶名和密碼,但它給SQL語法錯誤

You have an error in your SQL syntax; check the manual that corresponds to your MySQLserver version for the right syntax to use near 'AND UserPassword=' at line 1 

    mysql_select_db("emriphone", $con); 


    $u=$_GET['UserName']; 
    $pw=$_GET['UserPassword']; 

    $check ="SELECT UserName,UserPassword from appUsers WHERE UserName=$u AND UserPassword=$pw"; 

    $login=mysql_query($check,$con) or die(mysql_error()); 

    if(mysql_num_rows($login)==1){ 

    $row =mysql_fetch_assoc($login); 
    echo 'YES'; exit; 
    } 

else{ 
    echo'NO';exit; 
    } 

mysql_connect($con); 
+0

爲什麼你已經標記了這個問題的** ** iPhone? – Devang 2012-03-19 05:39:02

+0

,因爲我用iPhone應用程序連接這個數據庫並驗證密碼和用戶名 – user1263350 2012-03-19 06:23:31

+0

那麼是什麼?沒有任何與iPhone相關的代碼! – Devang 2012-03-19 06:32:38

回答

1

使用單引號變量在查詢''

希望這有助於你。

+0

我已經這樣做,但每次我的腳本發送否回顯 – user1263350 2012-03-19 06:21:30

+0

給如果(mysql_num_rows($登錄)> 0) – Ghostman 2012-03-19 06:33:11

+0

完成但同樣的問題 – user1263350 2012-03-19 06:36:24

3

您需要將單引號中查詢 -

$check ="SELECT UserName, UserPassword 
     FROM appUsers 
     WHERE UserName='$u'   
     AND UserPassword='$pw'";  // These are probably varchar data columns 
             // in your db. In that case, you should 
             // put single quotes like this. 

當你搜索數據庫中的文本字段,應該在它們周圍放置單引號。否則MySQL會報告它爲錯誤。

2

也許你需要在where子句中圍繞username和password參數添加單引號,因爲我假設它們是字符串。在MySQL中,你需要用單引號包裝這些字符串。

2

假設用戶名和密碼,你應該糾正如下

$check ="SELECT UserName,UserPassword from appUsers WHERE UserName=$u AND UserPassword=$pw"; 

$check ="SELECT UserName,UserPassword from appUsers WHERE UserName='$u' AND UserPassword='$pw'"; 

包括各地的用戶名和密碼

1
$check ="SELECT UserName,UserPassword from appUsers WHERE UserName=$u AND UserPassword=$pw"; 

單引號的文本字段都因爲你是使用mysql時,一定要清除SQL注入的用戶名和密碼使用以下內容:

$u = mysql_real_escape_string($_GET['UserName']); 
$pw = mysql_real_escape_string($_GET['UserPassword']); 

最後的想法;使用POST而不是GET的登錄頁面; d嘿嘿

0

從SQL注入保護自己,而你在它

$query = sprintf("SELECT UserName,UserPassword from appUsers WHERE UserName='%s' AND UserPassword='%s'", mysql_real_escape_string($u),mysql_real_escape_string($pw));