How to post data from iOS app to MySQL database?

前端 未结 3 429
独厮守ぢ
独厮守ぢ 2021-01-24 23:11

I saw a similar post to my question but his solution did not work for me for some odd reason and it is making me age faster than Obama.

Basically I want to post data fro

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-24 23:36

    As mapek already posted code for PHP , let me post answers for iOS part only. You can pass the parametres in POST like below.

    Method: 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);
    }];
    

    Method 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);
    }];
    

提交回复
热议问题