2014-12-22 3 views
2

mysql 데이터베이스에 다른 세부 정보가있는 이미지를 삽입하고 싶습니다. 데이터 값을 저장할 수 있지만 이미지가 삽입되지 않습니다. 아래 코드를 확인하십시오.mysql 데이터베이스에 이미지를 삽입하는 방법

(IBAction) addData:(id)sender; { 

    //Image upload 
    NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90); 
    NSString *urlString = @"http://localhost/myservice.php?"; 

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

    NSString *boundary = @"---------------------------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:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"Content-Type: application/octet-stream\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]; 

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
    NSLog(@"Image upload string%@",returnString); 


    //Upload data 
    NSString *url = [NSString stringWithFormat:[urlString stringByAppendingString:@"userName=%@&age=%@&phone=%@&image=%@"],txtName.text, txtAge.text, txtPhone.text,returnString]; 
    NSData *dataUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]; 

    NSString *strResult = [[NSString alloc] initWithData:dataUrl encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@",strResult); 


    if (![strResult isEqual: @""]) { 
     UIAlertView *alert = [[UIAlertView alloc] 
           initWithTitle: @"Record Saved" 
           message: @"Your Record is saved successfully" 
           delegate: nil 
           cancelButtonTitle:@"OK" 
           otherButtonTitles:nil]; 
     [alert show]; 


     //Clear fields 
     [email protected]""; 
     [email protected]""; 
     [email protected]""; 
     imageView.image= nil; 

    }else { 

     UIAlertView *alert = [[UIAlertView alloc] 
           initWithTitle: @"Record not Saved" 
           message: @"Your Record does not saved successfully" 
           delegate: nil 
           cancelButtonTitle:@"OK" 
           otherButtonTitles:nil]; 
     [alert show]; 

    }   
} 
+0

위의 Objective-C 코드가 PHP 코드가 아닌 것으로 의심되는 이유가 있습니까? 잘못된 것만 보이는 것은'HTTPBody'의 시작 부분에있는 선두의'\ r \ n '입니다. [Charles] (http://charlesproxy.com)와 함께이 요청을 잘 보일 수도 있지만 여기에는 명백하게 잘못된 점이 하나도 없습니다. 물론 동기식 요청을 사용해서는 안되며 추가 NSData 객체를 인스턴스화해서는 안되며 JPEG 표현을 검색하지 않는 것이 좋습니다. 그러나 그 중 어느 것도 거래 차단기가 아닙니다. – Rob

+0

FWIW, 이것은 이전에 사용한 코드입니다. http://stackoverflow.com/a/24252378/1271826 – Rob

+0

Rob..please 내 PHP 코드도 확인하십시오. – user3259655

답변

0

문제의 몇 :

  1. 귀하의 PHP 코드가 올바르지 않습니다. userfile$_FILES을 사용해야합니다. Handling File Uploads을 참조하십시오.

  2. 이진 데이터를 가져 와서 SQL 문을 작성할 수 없습니다. SQL에 ? 자리 표시자를 사용하고 업로드 된 이미지와 연관된 BLOB를 수동으로 mysqli_stmt::bind_param 또는 이와 동등한 것으로 수동으로 바인딩하려고합니다.

    솔직히 SQL 인젝션 공격으로부터 자신을 보호하기 위해 어쨌든 그렇게하는 것이 현명합니다.

  3. PHP 코드는 요청이 설정되어 있지 않은 변수 집합에 대해 $_GET을 참조합니다. 먼저 $_GET이 아닌 $_POST이어야하며, 둘째, 서버에 이러한 변수가 필요한 경우 요청에 설정해야합니다.

+1

감사합니다. Rob! 마지막으로 이미지를 서버에 업로드 할 수 있습니다. – user3259655

관련 문제