php - 无法通过 PHP 将 NSData 上传到 MySQL

标签 php mysql objective-c nsdata image-uploading

我正在为我的 iPhone 应用程序做服务器端工作,由于某种原因,imageData 将无法上传。如果您能看一下我的 PHP 代码,并告诉我是否有问题,我将不胜感激。

addImage.php 在调用 addrecipie 方法后调用。

我上传图像的 Obj-c 代码是:

NSString *name =  [@"Name: " stringByAppendingFormat:@"%d", arc4random()%20091011];
    NSString *username = [@"User: " stringByAppendingFormat:@"%d", arc4random()%20091011];
    NSString *description =  [@"Description: " stringByAppendingFormat:@"%d", arc4random()%20091011];
    NSString *shortDescription =  [@"ShortDescription:  " stringByAppendingFormat:@"%d", arc4random()%20091011];
    SBJsonWriter *writer = [[SBJsonWriter alloc] init];
    NSArray *array = [NSArray arrayWithObjects:@"llama", @"30", @"beef", @"beef", @"beefy", nil];
    NSString *ingredients = [writer stringWithObject:array];
    NSString *method = ingredients;
    NSString *category = @"Health";
    NSString *length = @"20";
    [writer release];
    NSArray *arr = [NSArray arrayWithObjects:name,description,shortDescription,ingredients,method,UIImageJPEGRepresentation([UIImage imageNamed:@"cupboard.jpg"], 1.0),username,length,category, nil];
    NSDictionary *dict = [[NSDictionary alloc] initWithObjects:arr forKeys:recipieKeys];
    [[AccountManager sharedManager] addRecipie:dict];
    [dict release];

- (NSString *)addRecipie:(NSDictionary *)recipie {
    NSData *data = [recipie objectForKey:@"image"];
    //form data away....

    NSString *url = [NSString stringWithFormat:@"http://...com/(..).php?name=%@&description=%@&shortDescription=%@&ingredients=%@&method=%@&username=%@&length=%@&category=%@", [recipie objectForKey:@"name"], [recipie objectForKey:@"description"], [recipie objectForKey:@"shortdescription"], [recipie objectForKey:@"ingredients"], [recipie objectForKey:@"method"], [recipie objectForKey:@"username"], [recipie objectForKey:@"length"], [recipie objectForKey:@"category"]];
    NSString *escaped = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];   
    [escaped stringByReplacingOccurrencesOfString:@"$" withString:@"%24"];
    [escaped stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
    [escaped stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
    [escaped stringByReplacingOccurrencesOfString:@"," withString:@"%2C"];
    [escaped stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"];
    [escaped stringByReplacingOccurrencesOfString:@":" withString:@"%3A"];
    [escaped stringByReplacingOccurrencesOfString:@";" withString:@"%3B"];
    [escaped stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
    [escaped stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
    [escaped stringByReplacingOccurrencesOfString:@"@" withString:@"%40"];
    [escaped stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    [escaped stringByReplacingOccurrencesOfString:@"\t" withString:@"%09"];
    [escaped stringByReplacingOccurrencesOfString:@"#" withString:@"%23"];
    [escaped stringByReplacingOccurrencesOfString:@"<" withString:@"%3C"];
    [escaped stringByReplacingOccurrencesOfString:@">" withString:@"%3E"];
    [escaped stringByReplacingOccurrencesOfString:@"\"" withString:@"%22"];
    [escaped stringByReplacingOccurrencesOfString:@"\n" withString:@"%0A"];
    ASIHTTPRequest *req = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:escaped]];
    [req startAsynchronous];
    if (![req error]) {
        NSString *ids = [req responseString];
        ASIFormDataRequest *reqs = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:[@"http://maxkdevelopment.co.uk/recipes/addimage.php?id=" stringByAppendingFormat:@"%@", [ids stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]]];
        [reqs setData:data forKey:@"image"];
        [reqs startAsynchronous];
        if (![reqs error]) {
            return [reqs responseString]; 
        }
        else {
            return [[reqs error] description]; 
        }
    }
    else {
        NSLog(@"%@", [[req error] description]);
    }
    return @"";
}

(addImage.php)

<?php
$id = $_GET['id'];
mysql_connect(...);
mysql_select_db("...") or die(mysql_error());
$query = "SELECT * FROM recipies WHERE id=$id";
$result = mysql_query($query);
if (mysql_num_rows($result) > 0) {
$sql = sprintf("UPDATE recipies SET Image = '%s' WHERE id=$id", mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"])));
$results = mysql_query($sql) or die(mysql_error());
$error = mysql_error();
echo "error: $error";
}
?>

(GetImage.php)

<?php
$id = $_GET['id'];
mysql_connect(...);
mysql_select_db(...");
$query = "SELECT * FROM recipies WHERE id=$id";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_assoc($result)) {
        echo $row['Image'];
    }
}
?>

(添加Recipe.php)

<?php
function uk_date() {
    $sign = "-"; 
    $h = "0"; 
    $dst = "true"; 
    if ($dst) {
        $daylight_saving = date('I');
        if ($daylight_saving){
           if ($sign == "-"){ $h=$h-1;  }
           else { $h=$h+1; }
      }
    }
    $hm = $h * 60;
    $ms = $hm * 60;
    if ($sign == "-"){ $timestamp = time()-($ms); }
    else { $timestamp = time()+($ms); }
    return $gmdate = gmdate("d/m/Y g:i:s A", $timestamp);
}
$name = $_GET['name'];
$description = $_GET['description'];
$shortDescription = $_GET['shortDescription'];
$ingredients = $_GET['ingredients'];
$category = $_GET['category'];
$method = $_GET['method'];
//$image = $_GET['image'];
$username = $_GET['username'];
$length = $_GET['length'];
$dateAdded = uk_date();
$conn = mysql_connect('...');
mysql_select_db('...');
$query = sprintf("INSERT INTO recipies (Name, Description, shortDescription, Ingredients, Method, Length, dateAdded, Username, Category) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
    mysql_real_escape_string($name),
    mysql_real_escape_string($description),
    mysql_real_escape_string($shortDescription),
    mysql_real_escape_string($ingredients),
    mysql_real_escape_string($method),
    mysql_real_escape_string($length),
    mysql_real_escape_string($dateAdded),
    mysql_real_escape_string($username),
    mysql_real_escape_string($category));

    $result = mysql_query($query) or die(mysql_error());

    $query2 = "SELECT * FROM `recipies` WHERE `Name` = $name AND `shortDescription` = $shortDescription";
    $result2 = mysql_query($query2) or die(mysql_error());
    if (mysql_num_rows($result2) > 0) {
        while($row = mysql_fetch_assoc($result2)) {
        die($row['id']);
        }
    }
?>

最佳答案

您已在 addImage.php SQL 中拼写了配方“recipie”。除非您的表名拼写错误,否则脚本将返回 0 行并且不运行后续查询。

关于php - 无法通过 PHP 将 NSData 上传到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5980854/

相关文章:

mysql - 有没有办法从 stat 中找到 mysql 列中值的唯一计数,例如 information_schema.tables 中的 table_rows ?

ios - 谷歌地图上未显示注释或标记

php - CodeIgniter 意外的 session 轮换

PHP、MySQL - 加载大约 100k 记录并将其导出到 xml

php - 根据总迭代次数更改我的 css 类中的宽度

php - 使用PHP更新MySQL数据库并返回值

PHP 注册未在表中创建数据

php - 将一个mysql表内容从一台服务器复制到另一台服务器

iphone - 后台服务是 "allowed to continue running while in the background"。那为什么我不能让它工作呢?

ios - iOS游戏每日奖励