Php web services not receiving data from iphone application

I connect to the php web service from my iphone application, I am doing a simple thing. 1. Getting user inputs for: username password in the text box from the iphone form and sending it to the PHP Post request web service. At the end of the web service, I get nothing but empty fields that are inserted into the MySQL database ........

Code for an example web service:

**********************SAMPLE CODE FOR WEB SERVICES************** <?php $con=mysql_connect("localhost","root","123456") or die(mysql_error()); mysql_select_db("eventsfast",$con); $username = $_REQUEST['username']; $password = $_REQUEST['password']; echo $username; echo $password; $data = $_REQUEST; $fp = fopen("log.txt", "w"); fwrite($fp, $data['username']); fwrite($fp, $data['password']); $sql="INSERT INTO users(username,password) VALUES('{$username}','{$password}')"; if(!mysql_query($sql,$con)) { die('Error:'.mysql_error()); } echo json_encode("1 record added to users table"); mysql_close($con); echo "test"; ?> ******************************PHP***************** ************** *****************************IPHONE EVENT CODE****************** #import "postdatawithphpViewController.h" @implementation postdatawithphpViewController @synthesize userName,password; -(IBAction) postdataid) sender { NSLog(userName.text); NSLog(password.text); NSString * dataTOB=[userName.text stringByAppendingString:password.text]; NSLog(dataTOB); NSData * postData=[dataTOB dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; NSLog(postLength); NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8888/write.php"]]; [request setURL:url]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; NSURLResponse *response; NSError *error; [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if(error==nil) NSLog(@"Error is nil"); else NSLog(@"Error is not nil"); NSLog(@"success!"); } ************************************************** ************** 

Please, help.............

+4
source share
1 answer

it seems that the parameter you pass in the iphone code is not correct

try it

 NSString *UserNamePara =[NSString stringWithFormat:@"username=%@",userName.text]; NSString *PasswordPara =[NSString stringWithFormat:@"password=%@",password.text]; NSString * dataTOB=[UserNamePara stringByAppendingFormat:@"&%@",PasswordPara]; 
0
source

Source: https://habr.com/ru/post/1306998/


All Articles