Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从iOS向Php发送字符串和数据,以便在MySQL数据库中创建表_Php_Ios_Mysql_Objective C_Json - Fatal编程技术网

如何从iOS向Php发送字符串和数据,以便在MySQL数据库中创建表

如何从iOS向Php发送字符串和数据,以便在MySQL数据库中创建表,php,ios,mysql,objective-c,json,Php,Ios,Mysql,Objective C,Json,我试图找到一种方法,将字符串和json数据文件传递给在线Php文件,该文件将使用字符串作为表名在MySQL数据库中创建一个表,并将json数据放入表中。到目前为止,我在iOS端有以下代码: NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *urlSession = [NSURLSession sessionWit

我试图找到一种方法,将字符串和json数据文件传递给在线Php文件,该文件将使用字符串作为表名在MySQL数据库中创建一个表,并将json数据放入表中。到目前为止,我在iOS端有以下代码:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    NSURL *url = [NSURL URLWithString:@"http://myWebAddress/uploadData.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:self.myData];

    NSURLSessionDataTask *sessionDataTask = [urlSession dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (error) {
            NSLog(@"%@", error);
        } else {
            self.textLabel.text=@"Data uploaded to database!";
        }
    }];
    [sessionDataTask resume];
在web端,我有以下Php代码:

<?php
$json_data=file_get_contents('php://input', true);
$post_data = json_decode($json_data);
$dbc=mysql_connect("mysql", "userName", "password");
mysql_select_db("myDatabase");
foreach($post_data as $lineData)
{$lastName=$lineData->lastName;
$firstName=$lineData->firstName;
$company=$lineData->company;
mysql_query("INSERT INTO `peopleDataTable` (`lastName`, `firstName`, `company`) VALUES ('$lastName', '$firstName', '$company')");}
mysql_close($dbc);
if (is_array($post_data))
$response = array("status" => "ok", "code" => 0, "original request" => $post_data);
else
$response = array("status" => "error", "code" => -1, "original_request" => $post_data);
$processed = json_encode($response);
echo $processed;
?>

请查看下面的链接以将数据作为参数传递,否则您可以使用AFNetwork进行数据传输,该网络具有非常简单的方法

你可以看到我的代码,我只是像下面这样调用这个方法

-(void)Login: (NSString *) User_fb_id
{
     NSMutableDictionary *api_call_parameter=[[NSMutableDictionary alloc] init];
      [api_call_parameter setObject:User_fb_id forKey:@"user_fb_id"];
      [api_call_parameter setObject:@"ios" forKey:@"device_type"];
      [api_call_parameter setObject:Device_Token forKey:@"device_id"];

      [self requestFunction:LOGIN :api_call_parameter :YES];
}

因此,您可以通过该字典将数据作为参数传递。

您可以使用JSON发送该字符串,并从php端获取。就像在self.myData中一样,添加键值对“tblName:name”,并在php端使用“$post_data->tblName”访问它。如果您仍有疑问,请告诉我,我会详细解释。@EktaMakadiya谢谢您的回复。我有两个详细的问题,你能帮我解答吗?首先,如何在iOS中将两个具有不同实体或字典类型的数组放入一个Json文件中?我应该先合并两个数组还是合并两个Json文件?其次,在php方面,如何为每种类型的实体挑选数据点?这就像你建议的“$post_数据->tblname”和“$post_数据->人员”一样吗?因此它将变成“foreach($post_data->tblname as$lineData)”。谢谢!谢谢你,卡蒂克先生。您知道如何将字符串和json数据对象与[NSMutableURLRequest set Value:]和[NSMutableURLRequest setHTTPBody:]一起发送吗?正确的语法应该是什么?
-(void)Login: (NSString *) User_fb_id
{
     NSMutableDictionary *api_call_parameter=[[NSMutableDictionary alloc] init];
      [api_call_parameter setObject:User_fb_id forKey:@"user_fb_id"];
      [api_call_parameter setObject:@"ios" forKey:@"device_type"];
      [api_call_parameter setObject:Device_Token forKey:@"device_id"];

      [self requestFunction:LOGIN :api_call_parameter :YES];
}