如何将数据从iOS应用程序发布到MySQL数据库?

我在我的问题上看到了一个类似的post,但他的解决方案因为一些奇怪的原因对我不起作用,这让我的年龄比奥巴马快。

基本上我想将数据从iOS应用程序发布到MySQL数据库。

iOS代码

NSString *strURL = [NSString stringWithFormat:@"http://www.example.com/phpfile.php?dishname=%@&description=%@",textfieldTwo.text, textfieldTwo.text]; NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]]; NSString *strResults = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@", strResults); 

PHP代码

  

数据库图像 在此处输入图像描述

我不知道为什么我遇到这个问题。 任何帮助将不胜感激,谢谢!

您正在通过GET传递参数。 所以你必须改变

 $dishname = $_POST['dishname']; $description = $_POST['description']; 

 $dishname = $_GET['dishname']; $description = $_GET['description']; 

在你的PHP脚本中。

由于mapek已经发布了PHP代码,让我发布iOS部分的答案。 您可以在POST中传递参数,如下所示。

方法:1

 NSData* submitData = [[NSString stringWithFormat:@"dishname=%@&description=%@",textfieldOne.text, textfieldTwo.text] dataUsingEncoding:NSUTF8StringEncoding]; NSMutableURLRequest *submitrequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.example.com/phpfile.php"]]; NSString *request = [[NSString alloc]initWithData:submitData encoding:NSUTF8StringEncoding]; NSLog(@"request is %@",request); [submitrequest setHTTPMethod:@"POST"]; [submitrequest setHTTPBody:submitData]; [NSURLConnection sendAsynchronousRequest:submitrequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"jsonString values=%@",jsonString); id values = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; NSLog(@"json values=%@",values); }]; 

方法2

  NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary]; [dictionnary setObject:textfieldOne.text forKey:@"dishname"]; [dictionnary setObject:textfieldTwo.text forKey:@"description"]; NSError *error = nil; NSData *submitData = [NSJSONSerialization dataWithJSONObject:dictionnary options:kNilOptions error:&error]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.example.com/phpfile.php"]]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setValue:@"json" forHTTPHeaderField:@"Data-Type"]; [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"]; [request setHTTPBody:jsonData]; [NSURLConnection sendAsynchronousRequest:submitrequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"jsonString values=%@",jsonString); id values = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; NSLog(@"json values=%@",values); }]; 

我想出来了,这是下面的代码。

PHP

  

iOS版

  NSString *strURL = [NSString stringWithFormat:@"http://www.example.com/register.php?firstname=%@&lastname=%@&password=%@&email=%@&",firstName.text, lastName.text, password.text, email.text]; NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]]; NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding]; NSLog(@"%@", strResult);