views:

8420

answers:

6

i create a NSURLRequest to post my data in the iphone application to a server to proceed the php script, my php script is look like this

<?php
$name = $_POST['name'];
$email = $_POST['email'];

$link = mysql_connect("localhost", "fffasfdas","Nfdsafafs") or die ("Unable to connect to database.");
mysql_select_db("muradsbi_mydatabase") or die ("Unable to select database.");

$sqlstatement= "INSERT INTO dbname (name,email) VALUES ('$name','$email')";
$newquery = mysql_query($sqlstatement, $link);
echo 'thanks for your register'; 
?>

and my NSURLRequst is create like below:

NSString *myRequestString = @"&name=Hello%20World&email=Ohai2u";
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ]
NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString: @"http://www.google.com/" ] ]; 
[ request setHTTPMethod: @"POST" ];
[ request setHTTPBody: myRequestData ];
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];

however, this site is unable to get the data from this application and save it to databse but i know it was connect succussfully because my application is able to get the response data from the server, i dont know whether is my variable name declare in wrongway or others issues can somebody point out for me

+10  A: 

You should remove the leading & in the myRequestString, and the problem is likely that the correct content-type header is not being sent. try adding a call to

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];

You should also not pass nil for error, so you can see what the client thinks is going on.

Unrelated, but your PHP code is open to sql injection attacks.

superfell
A: 

thanks i got it

A: 

I'm trying to do folowing:

$country = $_POST['Germany'];
$query = "SELECT * FROM world WHERE land='$country'";
$result = mysql_query($query) or die (mysql_error());

I just cannt get anything out from mySql. what could be the problem?

Poul John
A: 

it works, thanks for the solution!

Dzamir
A: 

Im trying to put the NSData gotten from the NSURLResponse and put it into an array. The gotten object should be a json encoded array.

mars
.... is this an answer? or a question? if it's the former, then it's not very helpful. if it's the latter, you should post a new question.
Dave DeLong
A: 

Also, you aren't releasing your memory... and you are missing some ";" characters.

PLEASE always post your actual cut/pasted code.

Patricia