2014-11-02 202 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