I send from an iOs Device a jsonString
That looks like this:
2012-10-10 08:50:32.011 Appname[4049:c07] Post String =http://www.yourdomain.nl/locatie.php?data=%7B%22id%22:%220612833397%22,%22longitude%22:%22-143.406417%22,%22latitude%22:%2232.785834%22,%22timestamp%22:%2210-10%2007:56%22%7D
That is when i NSLog it…
So the PHP file looks something like this:
<?php
$id = $_POST['id'];
$longitude = $_POST['longitude'];
$latitude = $_POST['latitude'];
$timestamp = $_POST['stringFromDate'];
$link = mysql_connect('server', 'sbla', 'bla')
or die('Could not connect: ' . mysql_error());
mysql_select_db('md267052db227433') or die('Could not select database');
// Performing SQL query
$query = "INSERT INTO locatie (id, longitude, latitude, timestamp) VALUES (NULL," . $longitude . "," . $latitude . "," .$timestamp." )";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo "OK";
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($link);
?>
When i go to yourdomain.nl/locatie.php it says that the it is empty, but that should be because i send the data from my iOs app.
I want that the jSonstring that i can see the id, longitude, latitude and timestamp and put them in my mySQL database, but it strangely doenst work.
I know that the string from the iOs device was sended.
Sorry for my bad english,
Any help would be appreciated.
EDIT
Send it like this:
- (void)myFuntionThatWritesToDatabaseInBackgroundWithLatitude:(NSString *)latitude longitude:(NSString *)longitude date:
(NSString *)stringFromDate {
_phonenumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];
NSMutableString *postString = [NSMutableString stringWithString:kPostURL];
NSString*jsonString = [[NSString alloc] initWithFormat:@"{\"id\":\"%@\",\"longitude\":\"%@\",\"latitude\":\"%@\",\"timestamp\":\"%@\"}",_phonenumber, longitude , latitude, stringFromDate];
[postString appendString:[NSString stringWithFormat:@"?data=%@", jsonString]];
[postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[[NSURLConnection alloc] initWithRequest:request delegate:self ];
NSLog(@"Post String =%@", postString);
// LocationTestViewController*locationTestViewController = [[LocationTestViewController alloc]init];
// phonenumber = locationTestViewController.telefoonnummer;
NSLog(@"telnr : %@", _phonenumber);
NSURLResponse* response;
NSHTTPURLResponse* httpResponse;
NSError* error;
NSData* responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString* stringResponse = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
httpResponse = (NSHTTPURLResponse*) response;
int statuscode = [httpResponse statusCode];
if (statuscode == 200)
{
NSLog(@"Verstuurd");
// Handle the response here if needed
}
else
{
NSLog(@"niet verstuurd: %@", stringResponse);
// Show some form of alert here if needed
}
// release all objects saved to memory
[request release];
request = nil;
[stringResponse release];
stringResponse = nil;
}
First, this is not JSON, its POST FORMDATA probably. Second, when you open the link (with a browser) – it becomes a GET request, not a POST. Try logging the $_POST in file when doing the request from the iOS device. Like this (first line of PHP file):
Then do several requests to see the results in “dump.txt” in same dir.
Also, the query should look like this (if I have guessed your colums right):
The PHP manual discourages usage of the
mysql_real_escape_stringthough – more on this here:http://php.net/manual/en/function.mysql-real-escape-string.php
EDIT:
My mistake, so the POSTed data is in fact JSON string – try this: