IOS连接到PHP Web服务器将数据插入mysql

IOS连接到PHP Web服务器将数据插入mysql,php,mysql,ios,json,post,Php,Mysql,Ios,Json,Post,我试图将NSDictionary(编码为Json格式)发布到webserver(由php处理), 在php中,我试图解码json数据并逐个检索变量, 然后将这些变量插入mysql数据库 以下是我的一些代码: -(void)postTest{ //build up the request that is to be sent to the server NSMutableURLRequest *request = [[NSMutableURLRequest alloc]

我试图将NSDictionary(编码为Json格式)发布到webserver(由php处理), 在php中,我试图解码json数据并逐个检索变量, 然后将这些变量插入mysql数据库 以下是我的一些代码:

    -(void)postTest{

    //build up the request that is to be sent to the server
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://localhost:8888/index.php"]];

    [request setHTTPMethod:@"POST"];
    [request addValue:@"postValues" forHTTPHeaderField:@"METHOD"];

    //create data that will be sent in the post
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
    [dictionary setValue:@"p7878" forKey:@"id"];
    [dictionary setValue:@"tmd" forKey:@"first_name"];
    [dictionary setValue:@"nmb" forKey:@"last_name"];
    [dictionary setValue:@"goog" forKey:@"industry"];

    //serialize the dictionary data as json
    NSData *data = [[dictionary copy] JSONValue];

    [request setHTTPBody:data]; //set the data as the post body
    [request addValue:[NSString stringWithFormat:@"%d",data.length] forHTTPHeaderField:@"Content-Length"];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if(!connection){
        NSLog(@"Connection Failed");
    }

}
我可以看到这些数据已成功发布到Web服务器。但不知何故,我无法反序列化json数据并将其存储在php上的不同变量中:

 <?php
// Create connection
$con=mysqli_connect("localhost","root","root","wenetwork");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

if ($_SERVER['HTTP_METHOD'] === 'postValues'){ 
   $body;
   /*Sometimes the body data is attached in raw form and is not attached 
   to $_POST, this needs to be handled*/
   if($_POST == null){
      $handle  = fopen('php://input', 'r');
      $rawData = fgets($handle);
      $body = json_decode($rawData);
   }
   else{
      $body == $_POST;
   }



   echo json_encode($body);//just return the post you sent it for testing purposes


    $id = $body['id'];
    $first_name = $body['first_name'];
    $last_name = $body['last_name'];
    $industry = $body['industry'];


    $insertProfile = "INSERT INTO basic_profile (id, first_name, last_name, industry) VALUES ('". $id ."','". $first_name ."','". $last_name ."','". $industry ."')";

    mysqli_query($con, $insertProfile) or die (mysqli_error("Insert item's profile failed"));

    echo "Item added successfully!";
}
 mysqli_close($con);
?>

我自己解决的问题,只需将php中的if语句替换为以下内容即可

    if ($_SERVER['HTTP_METHOD'] === 'postValues'){ 
   $jsonArray;
   /*Sometimes the body data is attached in raw form and is not attached 
   to $_POST, this needs to be handled*/
   if($_POST == null){
      $jsonString = file_get_contents('php://input');
      $jsonArray = json_decode($jsonString, true);
   }
   else{
      $jsonArray == $_POST;
   }



   echo json_encode($jsonArray);//just return the post you sent it for testing purposes



    // with [] instead of ()
    $email = $jsonArray['firstkey']; 

    $id = $jsonArray['id'];
    $first_name = $jsonArray['first_name'];
    $last_name = $jsonArray['last_name'];
    $industry = $jsonArray['industry'];


    $insertProfile = "INSERT INTO basic_profile (id, first_name, last_name, industry) VALUES ('". $id ."','". $first_name ."','". $last_name ."','". $industry ."')";

    mysqli_query($con, $insertProfile) or die (mysqli_error("Insert item's profile failed"));

    echo "Item added successfully!";
}

顺便说一句,与其使用
postValues
HTTP_方法(或
方法
…您可以同时使用这两种方法),我可能建议使用更常见的方法,例如
应用程序/json
内容类型
,或者类似的方法。另外,不要忘记使用或保护自己免受SQL注入攻击和带有撇号的值的常规错误。