【问题标题】:How to insert an image to the mysql database如何将图像插入mysql数据库
【发布时间】:2015-02-20 02:49:15
【问题描述】:

我想在 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
        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];

    }        
}

【问题讨论】:

  • 你怀疑上面的 Objective-C 代码而不是你的 PHP 代码有什么原因吗?唯一看起来错误的是HTTPBody 开头的前导\r\n。您可以使用Charles 观察此请求,以确保它看起来不错,但这里没有其他明显错误。当然,您不应该使用同步请求,不应该实例化额外的 NSData 对象,您可能希望避免检索 JPEG 表示等,但这些都不会破坏交易。
  • FWIW,这是我过去使用的代码:stackoverflow.com/a/24252378/1271826
  • Rob..请检查我的 php 代码。
  • 我没有看到 PHP 代码(至少现在还没有)。但不要只是用大量代码抛弃我们并期望我们能够诊断它。您确实需要准确地分享您所做的诊断,理想情况下生成一个可重现的问题示例。但是大量带有神秘“它不起作用”的代码并不是可接受的 S.O.问题。

标签: ios sqlite


【解决方案1】:

几个问题:

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

  2. 您不能获取二进制数据并从中构建 SQL 语句。您可能希望在 SQL 中使用 ? 占位符,然后手动将与上传的图像关联的 blob 与 mysqli_stmt::bind_param 或等效项绑定。

    坦率地说,为了保护自己免受 SQL 注入攻击,无论如何还是要谨慎行事。

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

【讨论】:

  • 谢谢罗伯!终于可以把图片上传到服务器了。
猜你喜欢
  • 1970-01-01
  • 2013-03-07
  • 1970-01-01
  • 1970-01-01
  • 2012-09-21
  • 2016-05-26
  • 2013-01-20
  • 2020-07-20
  • 2011-07-22
相关资源
最近更新 更多