Php 将数据发送到Objective中的服务器
我目前正试图将数据从我的xcode项目发送到我制作的服务器,但当我运行应用程序时,数据似乎没有出现在我的表中 它非常简单,允许用户输入一个名称和一条消息,当按下按钮时发送该消息。我不知道我做错了什么,有什么建议吗 以下是我的php代码:Php 将数据发送到Objective中的服务器,php,ios,objective-c,xcode,Php,Ios,Objective C,Xcode,我目前正试图将数据从我的xcode项目发送到我制作的服务器,但当我运行应用程序时,数据似乎没有出现在我的表中 它非常简单,允许用户输入一个名称和一条消息,当按下按钮时发送该消息。我不知道我做错了什么,有什么建议吗 以下是我的php代码: <?php $username = "root"; $database = "testdb"; mysql_connect('localhost', $username); @mysql_select_db($database) or die ("U
<?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();
?>
您在Objective C中执行post,因此在PHP中,您需要使用$\u post而不是$\u GET捕获变量。这就是您实际向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];
}
非常感谢你!我设法使它工作起来。然而,快速提问:如果我有多个视图控制器,它们有自己的输入框,我是否需要为每个VC创建php文件?如果您只使用收到的Post变量,一个文件就足够了:例如,
$firstFile=$\u Post[“sentFromVC1”]$第二个文件=$_POST[“sentFromVC2”];如果(第一个文件){echo“我们从ViewController1收到一个字符串”}如果(第二个文件){echo”我们从ViewController2收到一个字符串”}
#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
- (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];
}