gpt4 book ai didi

ios - 如何将图片插入mysql数据库

转载 作者:行者123 更新时间:2023-12-02 08:09:11 24 4
gpt4 key购买 nike

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

}
}

最佳答案

几个问题:

  1. 您的 PHP 代码不正确。您应该使用 $_FILES 作为 userfile。请参阅Handling File Uploads .

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

    坦率地说,无论如何,这样做都是谨慎的做法,以保护自己免受 SQL 注入(inject)攻击。

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

关于ios - 如何将图片插入mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27595323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com