2014-11-02 258 views
0

將數據發送到服務器目前我正在試圖從我的Xcode項目將數據發送給我做了,但數據似乎並沒有出現在我的表,當我跑我的應用程序的服務器。在的ObjectiveC

這是很簡單的,允許用戶輸入一個名稱,當按下一個按鈕,發送消息。我不知道我做錯了什麼,有什麼建議嗎?

這裏是我的PHP代碼:

<?php 

$username = "root"; 
$database = "testdb"; 

mysql_connect('localhost', $username); 

@mysql_select_db($database) or die ("Unable to find database"); 

$name = @$_GET["name"]; 
$message = @$_GET["message"]; 

$query = "INSERT INTO test VALUES ('', '$name', '$message')"; 

mysql_query($query) or die (mysql_error("error")); 

mysql_close(); 

?> 

和我的obj的C代碼:

.H

#import <UIKit/UIKit.h> 

#define kPostURL @"http://localhost/TESTCONNECT.php" //variable to use whenever 
#define kName @"name" 
#define kMessage @"message" 



@interface FirstViewController : UIViewController{ 

IBOutlet UITextField *nameText; 
IBOutlet UITextView *messageText; 

NSURLConnection *postConnection; 
} 

-(IBAction)post:(id)sender; 


@end 

.M

#import "FirstViewController.h" 

@interface FirstViewController() 

@end 

@implementation FirstViewController 

- (void)viewDidLoad { 
[super viewDidLoad]; 

} 




-(void) postMessage:(NSString*) message withName:(NSString *) name{ 

if(name !=nil && message !=nil){ 

    NSMutableString *postString = [NSMutableString stringWithString:kPostURL]; 
    [postString appendString:[NSString stringWithFormat:@"?%@=%@", kName, name]]; 

    [postString appendString: [NSString stringWithFormat:@"&%@=%@", kMessage, message]]; 

    [postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

    NSMutableURLRequest *request =[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString: postString]]; 


    [request setHTTPMethod:@"POST"]; 

    postConnection =[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES]; 


} 
} 

-(IBAction)post:(id)sender{ 
[self postMessage:messageText.text withName:nameText.text]; 
[messageText resignFirstResponder]; 
messageText.text=nil; 
nameText.text=nil; 

} 




- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 

} 

@end 

回答

0

你正在做後期在目標C中,所以在PHP中,你需要捕獲t他用$ _POST變量,而不是$ _GET。

0

這是你如何能夠實現發送一個字符串到PHP(POST)文件。我認爲其餘的應該工作。我沒有嘗試過,但對我來說看起來不錯。

- (void) send { 
    NSString *testString = @"this is a test"; 

    NSString *post =[NSString stringWithFormat:@"message=%@",testString]; 
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:[NSURL URLWithString:@"YOUR/URL/test.php"]]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPBody:postData]; 
} 
+0

非常感謝您!我設法讓它工作。但是,快速的問題:如果我有多個視圖控制器與他們自己的輸入框,我需要爲每個VC創建PHP文件? – Pammuelo 2014-11-03 03:54:30

+0

如果您只是使用您收到的Post-vars,則一個文件應該足夠: '$ firstFile = $ _POST [「sentFromVC1」]; $ secondFile = $ _POST [「sentFromVC2」]; if(firstFile){ echo「We received a string from ViewController1」; } 如果(secondFile){ 回聲 「我們收到了從串ViewController2」; }' – Michael 2014-11-03 06:55:21