php web services not getting data from iphone application

Posted by user317192 on Stack Overflow See other posts from Stack Overflow or by user317192
Published on 2010-04-15T06:28:02Z Indexed on 2010/04/15 6:33 UTC
Read the original article Hit count: 168

Filed under:

Hi, I am connecting with a php web service from my iphone application, I am doing a simple thing i.e. 1. Getting user inputs for: username password in a text field from the iphone form and sending the same to the PHP Post request web service. At the web service end I receive nothing other than blank fields that are inserted into the MySQL Database.......

The code for sample web service is: ***********SAMPLE CODE FOR WEB SERVICES*****

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.............

© Stack Overflow or respective owner

Related posts about iphone