2014-12-22 90 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。请检查我的php代码。 – user3259655

回答

0

几个问题:

  1. 你的PHP代码是不正确的。 userfile您应该使用$_FILES。见Handling File Uploads

  2. 你不能采取二进制数据,只是从它建立一个SQL语句。您可能希望在您的SQL中使用?占位符,然后手动将与上传图像关联的斑点与mysqli_stmt::bind_param或其他等效物绑定。

    坦率地说,无论如何这样做是谨慎的,以防止SQL注入攻击。

  3. PHP代码引用的$_GET是针对您的请求未设置的一堆变量。首先,应该是$_POST,而不是$_GET,其次,如果你的服务器需要这些变量,你应该在你的请求中设置它们。

+1

谢谢Rob!最后,我可以将图像上传到服务器。 – user3259655