I got a working code on getting the location.
I want to store the values from a textfield(i just leave it NULL for now), my longitude, latitude and timestamp.
This is the code where i send it to my php file
NSString *latitude = [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", newLocation.coordinate.longitude];
NSString *stringFromDate = [formatter stringFromDate:newLocation.timestamp];
NSMutableURLRequest *request =
[[NSMutableURLRequest alloc] initWithURL:
[NSURL URLWithString:@"http://www.yourdomain.com/locatie.php"]];
[request setHTTPMethod:@"POST"];
NSString* postString = [NSString stringWithFormat:@"%@ %@ %@", latitude, longitude, stringFromDate];
[request setValue:[NSString
stringWithFormat:@"%d", [postString length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[postString
dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
And here’s my code in my PHP File.
<?php
// Connecting, selecting database
$id = $POST['id'];
$longitude = $POST['longitude'];
$latitude = $POST['latitude'];
$timestamp = $POST['stringFromDate'];
$link = mysql_connect('server', 'name', 'pass')
or die('Could not connect: ' . mysql_error());
mysql_select_db('db_name') 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);
?>
What am i doing wrong?
All the help is appreciated.
Thanks
PS: I don’t get any errors, the only thing is that my database stays empty
It’s still not working, please if anybody knows what i’m doing wrong
In addition to the other answers about the
$_POSTglobal variable, I believe you have 2 PHP errors when you attempt to build your query string.You aren’t assigning the query string properly to the $query variable. You should use an equal sign to assign the string to the $query PHP variable. For example:
You need to concatenate the PHP variables ($longitude, $latitude, $timestamp) into the query string you are building using a period (
.), which is the PHP concatenation operator. The concatenation allows PHP to dynamically build the string.Depending on the format of the $timestamp string and datatypes in your table, you may need to wrap it in single quotes.
"'" . $timestamp . "'". Get some sample data for $longtitude, $latitude, $timestamp and try to write the SQL statement in phpMyAdmin or whatever DB tool you are using to see if you need quotes.Also, the SQL Injection issue mentioned earlier should also be addressed. You are not sanitizing the strings you assigned to the $longitude, $latitude, $timestamp PHP variables from the $_POST global variable. You’re passing them directly through to MySQL. This is where you can get into trouble if there are malicious statements in the strings.