Php Iphone到MySQL数据库的位置
我正在尝试向数据库发送一些值。 这是我在iOS中的代码和我的Php代码 我正在后台接收位置,并希望将其存储在我的数据库中 任何帮助都很好。 多谢各位Php Iphone到MySQL数据库的位置,php,mysql,ios,post,Php,Mysql,Ios,Post,我正在尝试向数据库发送一些值。 这是我在iOS中的代码和我的Php代码 我正在后台接收位置,并希望将其存储在我的数据库中 任何帮助都很好。 多谢各位 NSString *latitude = [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude]; NSString *longitude = [NSString stringWithFormat:@"%f", newLocation.coordinate.longi
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);
?>
非常感谢你
Greetspost string变量的赋值发生在右侧求值之后,因此当您在那里使用postString变量时,它只是堆栈上的垃圾,几乎肯定不是指向有效字符串的指针。只需像创建其他字符串一样创建该字符串:
// fix the formatting as necessary for your app
NSString* postString = [NSString stringWithFormat:@"%@ %@ %@", latitude, longitude, stringFromDate];
非常感谢你。至少我的应用程序不再暂停了。现在我将查看我的数据库,看看它是否存储了这些值。非常感谢。OK我检查了我的数据库,但它没有给我任何值。@davidraijmakers您是否正确地重新格式化了post字符串?我给出的答案修复了问题标题中的EXC_BAD_访问异常,但它不是格式正确的查询字符串。你需要添加变量赋值,这样你的服务器才能读取。我只修改了你的一小段代码。我真的不知道我做错了什么,我还修改了:$query(“插入到locatie(id,经度,纬度,时间戳)值(NULL,$longitude,$latitude,$timestamp)”;您的NSString*postString=[NSString stringWithFormat:@“%@%@%@”,纬度、经度、stringFromDate];
// fix the formatting as necessary for your app
NSString* postString = [NSString stringWithFormat:@"%@ %@ %@", latitude, longitude, stringFromDate];