0

I am sending data from iphone application to server using php but data is not inserted in the data base when i post the data in return it just sents the [] in echo.

My iPhone Code

    -(void)submitSurveyAnswers{


     NSString*survey_question_response_id="1";
     NSString*survey_id=@"1";
     NSString *question_id =@"1";
     NSString *survey_response_answer_id =@"1";
     NSString *post =[[NSString alloc] initWithFormat:@"survey_question_response_id=%@&survey_id=%@&question_id=%@&survey_response_answer_id=%@",survey_question_response_id,survey_id,question_id,survey_response_answer_id];
     NSLog(post);
    NSURL *url=[NSURL URLWithString:@"http://myserver-solutions.com/app/surveyAnswer.php?"];

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
   NSError *error;
   NSURLResponse *response;
   NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

  NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
  NSLog(@"%@",data);
  }

Here is the my php code

    <?php
    $host = ""; 
    $user = ""; 
    $pass = ""; 
    $database = ""; 

    $linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host."); 

    mysql_select_db($database, $linkID) or die("Could not find database."); 

    $survey_question_response_id=$_POST['survey_question_response_id'];
    $survey_id=$_POST['survey_id'];
    $question_id=$_POST['question_id'];
    $survey_response_answer_id=$_POST['survey_response_answer_id'];
    echo($survey_question_response_id);

    $query=("INSERT INTO survey_question_responses 

(survey_question_response_id,survey_id,question_id,survey_response_answer_id)

    VALUES ('$survey_question_response_id', '$survey_id','$question_id','$survey_response_answer_id')");

     mysql_query($query,$con);
     printf("Records inserted: %d\n", mysql_affected_rows());
     echo($survey_id)
     ?>
4

1 に答える 1

0

I have a full example on Github to dynamically create forms and submit them to a POST server. You may be able to use it depending upon how complex your forms are. Each form is described in a plist and is very dynamic.

If it is missing support for any desired form type (checkboxes, drop-down, etc.), let me know and I'd be happy to add them. (I only needed text-based input in my forms so far, but if there is a demand, I'd like to expand it to more complex items too!)

(A PHP file is also included to catch the response.)

https://github.com/mikecheckDev/MDContactForm

于 2012-08-06T21:43:00.207 に答える