I'm trying to find a way to pass a string and a json data file to an online Php file, which will create a table in a MySQL database using the string as the table name, and put the json data into the table. So far I have the following code on the iOS side:
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];
On the web side, I have the following Php code:
<?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;
?>
Could you help me to find a solution to modify this codes so that I can send a string to the Php, and use the string to specify the table name "peopleDataTable"?
Or I should completely modify the code?
Thank you very much!
Paul