2010-04-15 52 views
4

我從我的iPhone應用程序的PHP Web服務連接,我即做一個簡單的事情 1.獲取用戶輸入的:在文本字段從 用戶名密碼 iphone表單並將其發送到PHP Post請求Web服務。在Web服務端我收到無非就是插入MySQL數據庫的空白領域等.......PHP的Web服務不是從iPhone應用程序中獲取數據

樣品Web服務的代碼是:

**********************SAMPLE CODE FOR WEB SERVICES************** 
<?php 
$con=mysql_connect("localhost","root","123456") or die(mysql_error()); 

mysql_select_db("eventsfast",$con); 

$username = $_REQUEST['username']; 
$password = $_REQUEST['password']; 

echo $username; 
echo $password; 

$data = $_REQUEST; 



$fp = fopen("log.txt", "w"); 
fwrite($fp, $data['username']); 
fwrite($fp, $data['password']); 

$sql="INSERT INTO users(username,password) VALUES('{$username}','{$password}')"; 

if(!mysql_query($sql,$con)) 
{ 
die('Error:'.mysql_error()); 
} 

echo json_encode("1 record added to users table"); 
mysql_close($con); 

echo "test"; 
?> 
******************************PHP***************** ************** 

*****************************IPHONE EVENT CODE****************** 
#import "postdatawithphpViewController.h" 

@implementation postdatawithphpViewController 
@synthesize userName,password; 
-(IBAction) postdataid) sender 
{ 
NSLog(userName.text); 
NSLog(password.text); 
NSString * dataTOB=[userName.text stringByAppendingString:password.text]; 
NSLog(dataTOB); 
NSData * postData=[dataTOB dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; 

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
NSLog(postLength); 
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8888/write.php"]]; 
[request setURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 

NSURLResponse *response; 
NSError *error; 
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

if(error==nil) 
NSLog(@"Error is nil"); 
else 
NSLog(@"Error is not nil"); 
NSLog(@"success!"); 



} 

************************************************** ************** 

請幫助... ..........

+1

我認爲這是重複的問題。注意這一點。 http://stackoverflow.com/questions/2643247/unable-to-get-data-from-iphone-application – 2010-04-15 07:10:41

回答

0

看來你是傳遞iphone代碼參數不正確

試試這個

NSString *UserNamePara =[NSString stringWithFormat:@"username=%@",userName.text]; 
NSString *PasswordPara =[NSString stringWithFormat:@"password=%@",password.text]; 
NSString * dataTOB=[UserNamePara stringByAppendingFormat:@"&%@",PasswordPara]; 
相關問題