2012-09-03 29 views
0

我想向数据库发送一些值。 这是我的iOS代码和我的PHP代码。Iphone的位置到MySQL数据库

我recieving在背景中的位置,并希望这些信息存储在我的数据库,

任何帮助将是很好。 谢谢

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:固定

NSString *postString = [postString stringByAppendingFormat: latitude, longitude, stringFromDate]; **FIXED** 

谢谢柠了。

电贺

回答

1

右侧后评估你的文章的字符串变量的分配情况,所以当你使用postString变量存在,它只是什么垃圾是在堆栈上,几乎可以肯定不是指向有效串。简单地创建该字符串就像你做你的其他人:

// fix the formatting as necessary for your app 
NSString* postString = [NSString stringWithFormat:@"%@ %@ %@", latitude, longitude, stringFromDate]; 
+0

非常感谢你。至少我的应用程序不再停顿。我现在将查看我的数据库以查看它是否存储值。谢谢! –

+0

Oke我检查了我的数据库,但它没有给我任何值。 –

+0

@davidraijmakers是否正确地重新格式化了您的帖子字符串?我给出的答案修正了问题标题中的EXC_BAD_ACCESS异常,但它不是格式正确的查询字符串。您需要添加变量赋值,并且您的服务器将读取。 –