简体   繁体   English

如何从iPhone应用程序向服务器发送数据

[英]how to send data to server from iphone application

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. 我正在使用php将数据从iPhone应用程序发送到服务器,但是当我以返回形式发布数据时,数据没有插入数据库中,它只是在回显中发送了[]。

My iPhone Code 我的iPhone代码

    -(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代码

    <?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) (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)
     ?>

I have a full example on Github to dynamically create forms and submit them to a POST server. 我在Github上一个完整的例子来动态地创建表单并提交到服务器POST。 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. 每种形式都在plist中进行了描述,并且非常动态。

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.) (还包括一个PHP文件来捕获响应。)

https://github.com/mikecheckDev/MDContactForm https://github.com/mikecheckDev/MDContactForm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM