Insert data into remote mysql database by POST method from iOS device

后端 未结 3 1313
故里飘歌
故里飘歌 2021-01-14 20:26

I have 3 values :

  • id
  • name
  • email

I have three UIText field where i can give these input and save these values into

3条回答
  •  死守一世寂寞
    2021-01-14 20:49

    Can you try this? may be it works for you.

    NSString *strURL = [NSString stringWithFormat:@"name=%@&email=%@", nameTextField.text, emailTextField.text];
    
    NSData *postData = [strURL dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"http://mydomain.com/iOS/Tulon/phpFile.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
    [request setHTTPBody:postData];
    
    
    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    
    if(conn)
    {
        NSLog(@"Connection Successful");
    }
    else
    {
        NSLog(@"Connection could not be made");
    }
    

    After this implement following methods to get response

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)d
    {
         [self.ask_data appendData:d];
    
    }
    
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
    
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
    
        NSString *responseText = [[NSString alloc] initWithData:self.ask_data encoding:NSUTF8StringEncoding];
        NSLog(@"response=%@",responseText);
    }
    

提交回复
热议问题