2014-07-15 2 views
0

이렇게 복잡하지는 않아야한다고 생각하지만 여기서는 지난 2 일간이 작업을 수행하는 데 소요 된 후입니다. 내 iOS 응용 프로그램에서 서버에있는 PHP 스크립트에 두 개의 변수를 보내려고합니다. 데이터베이스가 항목을 추가하지만 2 개의 정보 필드가 비어 있습니다. 나는 Objective 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]; 
관련 문제