2014-07-15 65 views
0

我觉得这不应该是这么复杂,但在这之后我花了两天的时间试图让它工作。我试图从我的iOS应用程序发送两个变量到我在服务器上的一个php脚本。该数据库添加一个条目,但2个信息字段为空。我没有客观的C但是PHP新手。从iOS应用程序发送数据到PHP脚本

PHP代码

<?php 
     $name = $_POST['name']; 
     $comment = $_POST['comment']; 
        mysql_connect("Server","Username","Password"); 
        mysql_select_db("comments"); 
        mysql_query("INSERT INTO comments (id, name, comment) VALUES ('','$name','$comment')"); 
?> 

目标C代码

NSString *name = @"Name1"; 
    NSString *comment = @"Comment1"; 
    NSString *myRequestString = [NSString stringWithFormat:@"&name=%@&comment=%@",name,comment]; 
    NSData *myRequestData =[myRequestString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    NSString *postLength = [NSString stringWithFormat:@"%d",[myRequestData length]]; 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://www.notguiltyapp.com/harris.php"]]; 

    [request setHTTPMethod: @"POST"]; 

    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 

    // Set Request Body 
    [request setHTTPBody: myRequestData]; 
    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 

    if(conn) 
    { 
     NSLog(@"Connection Successful"); 
    } 
    else 
    { 
     NSLog(@"Connection could not be made"); 
    } 
+0

取代它没有工作。你为什么要为每个变量添加引号? – user3157910

回答

0

更新:我有更多的时间后,解决了这一问题。我拿出

NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 

NSData *returnData=[NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err]; 
相关问题