2012-09-03 30 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異常,但它不是格式正確的查詢字符串。您需要添加變量賦值,並且您的服務器將讀取。 –