2013-03-04 29 views
1

我想插入到一個MySQL數據庫連同其他值的blob,其他值進入罰款,並沒有任何錯誤。允許的最大數據包大小設置爲10mb,照片數據爲.5mb。iphone到mysql blob不插入沒有錯誤

這裏是照片中的二進制轉換代碼:

- (NSData *)dataFromImage:(UIImage *)image{ 
    NSData *imageData = UIImagePNGRepresentation(image); 
    return imageData; 
} 

則然後張貼到PHP腳本:

$db = new mysqli(localhost, $database, $pass, $table); 

if ($query = $db->prepare("INSERT INTO JobData (someotherdata, photo) values (? ,?)")) { 

    $query->bind_param("sb", 
      $_POST["someotherdata"], 
      $_POST["photo"]); 

    $query->execute(); 

的otherdata在數據庫中顯示出來,但0字節秀在照片領域。 mysqli_error不會產生任何錯誤。

NSData的日誌:

4f1eba47 4f37e5f1 d3475bf6 d9fef291 7b1cc893 af9e38c9 d3674fdd 13e4e9b3...etc 

也許我沒有正確的數據變成二進制?我無法在網上找到很多關於這方面的信息,大部分我發現與sqlite有關,因此非常感謝所有幫助。

我也檢查,看看是否所有的數據先於bind_param呼叫

+0

假設$ _POST [「photo」]在bind_param()調用之前確實包含內容是否安全? – Scuzzy 2013-03-04 01:30:57

+0

它只發布到一次 – user1899201 2013-03-04 01:53:51

回答

3

我最近在做一個類似的項目中,我需要上傳大量信息設置(姓名,身份證,年齡等)到一個伴隨着圖像的sql數據庫。遺憾的是它並不容易,因爲只是將數據轉換成二進制和發送,但有一個要命的一段代碼,會爲你做..

//this is where you put in the data e.g... 
NSMutableDictionary* _params = [[NSMutableDictionary alloc] init]; 
[_params setObject:userName forKey:@"sendername"]; 
//[_params setObject:[NSString stringWithFormat:@"%d", userId] forKey:[NSString stringWithString:@"userId"]]; 
//[_params setObject:[NSString stringWithFormat:@"%@",_title] forKey:@"title"]; 

// the boundary string : a random string, that will not repeat in post data, to separate post data fields. 
NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy"; 

// string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ 
NSString* FileParamConstant = @"picture"; 

// the server url to which the image (or the media) is uploaded. Use your server url here 
NSURL* requestURL = [NSURL URLWithString:@"url name here"]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; 
[request setHTTPShouldHandleCookies:NO]; 
[request setTimeoutInterval:30]; 
[request setHTTPMethod:@"POST"]; 

// set Content-Type in HTTP header 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant]; 
[request setValue:contentType forHTTPHeaderField: @"Content-Type"]; 

// post body 
NSMutableData *body = [NSMutableData data]; 

// add params (all params are strings) 
for (NSString *param in _params) { 
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]]; 
} 

// add image data 
UIImage *imageToPost = [UIImage imageNamed:@"image is here"]; 

NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0); 
if (imageData) { 
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:imageData]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
} 

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]]; 

// setting the body of the post to the reqeust 
[request setHTTPBody:body]; 

// set the content-length 
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 

// set URL 
[request setURL:requestURL]; 

NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; 



[connection start]; 

給一個嘗試,讓我知道你怎麼弄在......這可能不是你想要的,但希望它能引導你朝着正確的方向前進。 T

+0

我正在使用網絡連接,數據發佈正確,照片迷失在PHP腳本和數據庫之間,但其他數據經過。看起來你和我一樣,只是沒有圖書館,雖然我不確定,因爲我從來沒有使用過NSURLConnection。 – user1899201 2013-03-05 03:34:10

+0

甜蜜的男人,那麼我會讓我們的PHP男人看看你,因爲我知道他只是有一個問題,讓圖像保存在數據庫中。我認爲他提出的解決方案是將圖像保存到tmp文件夾中,然後將其傳輸到域的子文件夾中。然後,slqDB只是將參考url保存到可以在應用程序中訪問的圖像。生病讓他添加一些信息。 – 2013-03-05 04:15:12

+0

這就是我最初想做的,但公司想要在數據庫中的實際照片。我可能最終會按照你的方式去做。 – user1899201 2013-03-05 18:23:06