无法通过PHP将NSData上载到MySQL

无法通过PHP将NSData上载到MySQL,php,mysql,objective-c,nsdata,image-uploading,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: " strin

我正在为我的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 @"";
}
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*数组=[NSArray数组,其对象为:@“llama”@“30”@“beef”@“beefy”@“beefy”,nil];
NSString*Components=[writer stringWithObject:array];
NSString*方法=成分;
NSString*类别=@“健康”;
NSString*长度=@“20”;
[作者发布];
NSArray*arr=[NSArray arrayWithObjects:名称、描述、简短描述、成分、方法、UIImageJPEGresentation([UIImageNamed:@“cupboard.jpg”],1.0)、用户名、长度、类别、零];
NSDictionary*dict=[[NSDictionary alloc]initWithObjects:arr forKeys:recipieKeys];
[[AccountManager sharedManager]添加收件人:dict];
[听写释放];
-(NSString*)添加recipie:(NSDictionary*)recipie{
NSData*数据=[recipie objectForKey:@“image”];
//将数据从表格中删除。。。。
NSString*url=[NSString stringWithFormat:@”http://...com/(..).php?name=%@&description=%@&shortDescription=%@&Components=%@&method=%@&username=%@&length=%@&category=%@“、[recipie objectForKey:@“name”]、[recipie objectForKey:@“description”]、[recipie objectForKey:@“shortDescription”]、[recipie objectForKey:@“Components”]、[recipie objectForKey:@“方法”],[recipie objectForKey:@“用户名”],[recipie objectForKey:@“长度”],[recipie objectForKey:@“类别”];
NSString*转义=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[转义字符串byreplacingoccurrencesofstring:@“$”with string:@“%24”];
[通过replacingoccurrencesofstring:@”和“with string:@”%26“]转义的string;
[使用字符串@“%2B”替换字符串@“+”时发生的转义字符串];
[通过replacingoccurrencesofstring:@”,“with string:@”%2C“]转义的string;
[通过replacingoccurrencesofstring:@”/“with string:@”%2F“]转义的string;
[通过replacingoccurrencesofstring:@:“with string:@”%3A“]转义的string;
[通过replacingOccurrencesofString:@;通过字符串“@”%3B“@”转义的字符串;
[通过replacingoccurrencesofstring:@“=”和string:@“%3D”进行转义的stringByReplacingOccurrencesOfString:@“=”;
[使用字符串@“%3F”的字符串@“?”的replacingoccurrencesofString:@”转义字符串;
[通过replacingoccurrencesofstring:@“@”和string:@“%40”转义的stringByReplacingOccurrencesOfString:@“@”;
[使用字符串@“%20”替换字符串@的字符串@发生的转义字符串];
[使用字符串@“%09”的字符串@“\t”的重复替换发生率转义字符串];
[通过替换字符串发生的转义字符串:@“#”和字符串:@“%23”];
[使用字符串@“%3E”的字符串@替换发生的转义字符串];
[使用字符串@“%22”的字符串@“\”的replacingoccurrencesofstring:@“\”转义字符串;
[通过替换字符串发生的转义字符串:@“\n”和字符串:@“%0A”];
ASIHTTPRequest*req=[AsiHttpRequestWithURL:[NSURL URLWithString:转义]];
[请求启动同步];
如果(![req错误]){
NSString*ids=[req responseString];
ASIFormDataRequest*reqs=[AsiformDataRequestRequestWithURL:[NSURL URLWithString:[@]http://maxkdevelopment.co.uk/recipes/addimage.php?id=“stringByAppendingFormat:@”%@,[ids stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet][3];
[需求设置数据:数据分叉:@“图像”];
[要求启动同步];
如果(![reqs错误]){
返回[reqs responseString];
}
否则{
返回[[reqs error]description];
}
}
否则{
NSLog(@“%@,[[req错误]说明]);
}
返回@”;
}
(addImage.php)


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

尽管您在整个过程中似乎都在检查表名的命名方式。不过我要检查一下。是的,我知道。我把数据库命名错了。我搜索了我的代码,问题似乎是addrecipie.php脚本中没有回显id。我收到错误:
您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,了解要使用的正确语法:2964774和
shortDescription`=shortDescription:1988287``您需要用引号将字符串括起来。将
中的`Name`=$Name和`shortDescription`=$shortDescription
更改为其中的`Name`='$Name'和`shortDescription`='$shortDescription'`非常出色,工作非常完美。谢谢你,本!当您对请求调用
startAsynchronous
时,您是否意识到即使请求未完成,代码仍将继续超过该点?尝试使用
startSynchronous
(不确定这是否是正确的方法,现在无法检查)。是的,您是正确的。我对它进行了编辑,但php脚本返回所添加配方的id肯定是一个错误。
<?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";
}
?>
<?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'];
    }
}
?>
<?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']);
        }
    }
?>