2010-05-27 29 views
2

夥計們,我想在Web服務器上發佈數據到Web服務器,如用戶名和密碼,我使用PHP來回應是或否,附上所有代碼。任何人都可以幫我解決問題,代碼有什麼問題。因爲它總是說不正確的密碼或用戶名。我試圖用裏面的用戶名和密碼來測試php代碼,它正在工作。所以請幫助我。謝謝。Iphone sdk,如何發佈數據到網絡服務器?有人有例子嗎?

頭文件

@interface kiksyloginViewController : UIViewController { 
    IBOutlet UITextField *usernameField; 
    IBOutlet UITextField *passwordField; 
    IBOutlet UIButton *loginButton; 
} 

@property (nonatomic, retain) UITextField *usernameField; 
@property (nonatomic, retain) UITextField *passwordField; 
@property (nonatomic, retain) UIButton *loginButton; 

- (IBAction) login: (id) sender; 

@end 

實現文件

#import "kiksyloginViewController.h" 

@implementation kiksyloginViewController 

@synthesize usernameField; 
@synthesize passwordField; 
@synthesize loginButton; 

- (IBAction) login: (id) sender 
{ 

    NSString *post =[NSString stringWithFormat:@"username=%@&password=%@",usernameField.text, passwordField.text]; 

    NSString *hostStr = @"http://localhost/userlogin.php"; 
    hostStr = [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]; 

    } 
} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn’t have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren’t in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 

@end 

PHP文件

<?php 
//$Container = new Container; 

$u = $_GET['username']; 
$pw =$_GET['password']; 
$check = "select username, password from user where username='$u' and password='$pw'"; 
function myDbconn() 

{ 
    mysql_connect("localhost", "root","") or die(); 
    mysql_select_db("test") or die (mysql_error()); 
} 

myDbconn(); 


$login = mysql_query($check) or die (mysql_error()); 
//$run = DB()->select($check); 



if (mysql_num_rows($login)==1){ 
    //$row = DB()->fetch($run); 
    //$row = DB()->fetch($login); 
    $row = mysql_fetch_assoc($login); 
    echo 'yes'; 
    exit;   
} 
else { 
    echo 'No'; 
    exit; 
} 

?> 

回答

1

第一個問題,我看到這可能是最大的,有沒有? 「」任何地方。具體做法是:

@"username=%@&password=%@" 

應該看起來更像是這樣的:

@"?username=%@&password=%@" 

因爲你沒有真正發送到服務器,你僅僅使用GET請求和URL參數。

我不知道你是否打算在後臺運行你的設備上的web服務器和PHP,但是在某些時候我假設你會將http://localhost更改爲其他東西,並且希望你在之前會對SQL查詢進行清理您可以讓用戶瀏覽您的網頁並在網址查詢中輸入他們想要的任何內容。

1

的職位是舊的,但無論如何,如果它可以幫助別人:

呀,它有一對夫婦的錯誤。首先,在文件的.m正確的字符串應該是:

NSString *post =[NSString stringWithFormat:@"?&username=%@&password=%@", 
       usernameField.text, passwordField.text]; 

此外,在文件的.m:

if([serverOutput isEqualToString:@"yes"]){ 

,而不是 「是」。注意大寫字母!

這就是全部。代碼運行良好。

相關問題