我正在尝试将一些值发送到数据库。这是我在 iOS 中的代码和我的 PHP 代码。
我正在后台接收位置,并希望将其存储在我的数据库中,
你能帮忙的话,我会很高兴。谢谢
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/location.php"]];
[request setHTTPMethod:@"POST"];
NSString *postString = [postString stringByAppendingFormat: latitude, longitude, stringFromDate];
[request setValue:[NSString
stringWithFormat:@"%d", [postString length]]
forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[postString
dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
[httpResponse statusCode];
}
还有我的 PHP 文件
<?php
// Connecting, selecting database
$id = $POST['id'];
$longitude = $POST['longitude'];
$latitude = $POST['latitude'];
$timestamp = $POST['stringFromDate'];
$link = mysql_connect('server', 'user', '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 table 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);
?>
我在这一行得到 EXC_BAD_ACCES:FIXED
NSString *postString = [postString stringByAppendingFormat: latitude, longitude, stringFromDate]; **FIXED**
非常感谢你。
问候