2011-05-03 78 views
0

我有一個iPhone應用程序,我的數據通過GET發佈到服務器上的PHP文件。但是,在用奇怪的字符執行之後,沒有服務器響應。 (我把NSLog放在我從服務器返回的東西上)。將奇怪的字符發佈到PHP腳本中?

以下是我後我的數據到服務器:

// St uploading to server 
     NSString *urlString = [NSString stringWithFormat:@"http://myflashpics.com/iphone_processes/upload.php?album=%@&id=%@&caption=%@&orient=%@&size=%d&device=iPhone",getAlbumID,theUserID,theCaption,getOrientation,imageSize]; 

     NSLog(@"Calling URL: %@", urlString); 

     NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
     [request setURL:[NSURL URLWithString:urlString]]; 
     [request setHTTPMethod:@"POST"]; 

     NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
     NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 
     [request addValue:contentType forHTTPHeaderField:@"Content-Type"]; 

     NSMutableData *body = [NSMutableData data]; 
     [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[[NSString stringWithString:@"Content-Type: binary\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[NSData dataWithData:imageData]]; 
     [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [request setHTTPBody:body]; 

這裏就是我如何處理服務器端:

<?php 

// Connect to MYSQL 

$name = $_GET['name']; 

mysql_query("INSERT INTO *** (name) VALUES ('$name')"); 

我真的想,如果所有可能解決這個問題的服務器端。我提交給蘋果,我希望修復而不重新提交。

請請幫忙!由於

庫爾頓

+0

我想你想要$ _POST而不是$ _GET。 – Fosco 2011-05-03 05:30:15

+0

@Fosco:發佈一行代碼。請看看它。 – iosfreak 2011-05-03 05:31:21

+0

難道你沒有[已經問過這個問題](http://stackoverflow.com/questions/5865049/weird-characters-in-php-get)?你爲什麼要刪除它? – alex 2011-05-03 05:31:45

回答

1

如果你正在使用$ _GET,你應該在發送它們之前對你的變量進行編碼。

不管怎麼說,你應該做兩件事情:

  • 逃脫你的請求變量(GET或POST)與功能,如mysql_escape_string(http://php.net/manual/en/function.mysql-實時逃生string.php)插入到數據庫
  • 編碼的變量怪異字符的數字Unicode字符之前(我beleive即使用mb_convert_encoding($variable, 'HTML-ENTITIES', 'UTF-8');正確的術語)將它們存儲在數據庫之前

希望在幫助。

3

首先,如果你的行動正在創造一個記錄,你應該做一個POST,而不是GET。其次,停止使用mysql_ *函數並使用帶有參數化查詢的PDO。

如果您仍然遇到問題,請將php error_log()函數與var_export($ var,true)結合使用來寫出日誌消息以確定要檢索的PHP。

這與PDO生成的任何錯誤/異常相結合,可以指示問題出在哪裏。

+0

感謝您的迴應!如何使用我提供的代碼發佈所有變量?另外,有沒有辦法讓所有我得到它只是修復它的服務器端工作?我將我的應用提交給蘋果公司,這是我的第二個鏡頭,我不想再重新提交,再等兩週讓他們開始審覈。 – iosfreak 2011-05-03 05:45:52

+0

這是[PHP PDO教程](http://webcache.googleusercontent.com/search?q=cache:3qMh8k_v2U8J:www.phpro.org/tutorials/Introduction-to-PHP-PDO.html+php+pdo&cd=3&hl= = sv&ct = clnk&gl = se&source = www.google.se)\ [Google Cache \]。參見' - > bindParam()',這個例子是用'SELECT'語句創建的,但是也適用於'INSERT'。 – joar 2011-05-03 06:00:04

+0

嗨,你能幫我把我的變量和我的POST請求(我提供的第一位代碼)集成起來。我決定我會做最好的,然後重新提交。 – iosfreak 2011-05-03 06:06:15