2

I would like to insert an image with other details in mysql database. I am able to save data values but an image is not getting inserted. Please check my code below.

(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
        txtName.text=@"";
        txtAge.text=@"";
        txtPhone.text=@"";
        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];

    }        
}
Hemang
  • 26,840
  • 19
  • 119
  • 186
  • Is there some reason why you suspect the above Objective-C code rather than your PHP code? The only thing that looks wrong is the leading `\r\n` at the start of the `HTTPBody`. You might observe this request with [Charles](http://charlesproxy.com) to make sure it looks good, but there's nothing else obviously wrong here. Sure, you shouldn't use synchronous requests, you shouldn't instantiate additional `NSData` object, you might want to avoid retrieving JPEG representation, etc., but none of that is a deal breaker. – Rob Dec 22 '14 at 01:33
  • FWIW, this is the code I have used in the past: http://stackoverflow.com/a/24252378/1271826 – Rob Dec 22 '14 at 01:36
  • Rob..please check my php code as well. – user3259655 Dec 22 '14 at 01:38
  • I don't see PHP code (at least not yet). But don't just dump us with a ton of code and expect us to diagnose it. You really need to share precisely what diagnostics you've done, ideally producing a reproducible example of the problem. But a ton of code with a cryptic "it doesn't work" is not an acceptable S.O. question. – Rob Dec 22 '14 at 01:42

1 Answers1

0

A couple of issues:

  1. Your PHP code is not correct. You should use $_FILES for userfile. See Handling File Uploads.

  2. You cannot take binary data and just build a SQL statement from it. You probably want to use ? placeholders in your SQL and then manually bind the blob associated with the uploaded image with mysqli_stmt::bind_param, or something equivalent.

    Frankly, it's prudent to do that anyway, to protect yourself against SQL injection attacks.

  3. The PHP code is referencing $_GET for a bunch of variables that your request is not setting. First, that should be $_POST, not $_GET, and, second, if your server needs those variables, you should set them in your request.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044