我找到了一些信息here和here,但我没有找到关于此事的教程或好书。
由于种种原因,我不想使用Parse所以我决定尝试自己编写Web服务代码。 (我希望这是命名它的正确方法)。
我买了不同的书,虽然我很好地解释了我应该如何使用JSON或XML从数据库中检索数据,但我找不到有关数据插入的明确内容。
这就是我最终设法将我的数据从iphone应用程序插入我的数据库的方式。
XCODE:
-(IBAction)addData:(id)sender{
[self displayActivityIndicator];
NSString *country = self.countryLabel.text;
NSString *location = self.locationTextField.text;
NSString *city = self.cityTextField.text;
NSString *distance = self.distanceTextField.text;
NSString *max_part = self.partcipantsTextField.text;
NSString *pace = self.paceField.text;
NSString *rawStr = [NSString stringWithFormat:@"country=%@&location=%@&&city=%@&distance=%@&pace=%@&partecipant=%@", country,
location,
city,
distance,
pace,max_part];
NSData *data = [rawStr dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:@"http://www.mywebsite.com/savedata.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:data];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *responseString = [NSString stringWithUTF8String:[responseData bytes]];
NSLog(@"%@", responseString);
NSString *success = @"success";
[success dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%lu", (unsigned long)responseString.length);
NSLog(@"%lu", (unsigned long)success.length);
[self dismissViewControllerAnimated:YES completion:nil]; // Dismiss the viewController upon success
}
SAVEDATA.PHP
<?php
header('Content-type: text/plain; charset=utf-8');
$db_conn = new PDO('mysql:host=localhost;dbname=mydatabase','admin','password');
$db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$message = "";
$user = @"testUser";
$country = ($_POST['country']);
$city = ($_POST['city']);
$location = ($_POST['location']);
$distance = ($_POST['distance']);
$pace = ($_POST['pace']);
$part = ($_POST['partecipant']);
$qry = $db_conn->prepare('INSERT INTO myTable(`user_id`,`country`,`city`,`location`,`distance`,`pace`,`max_number`) VALUES (:user,:country,:city,:location,:distance,:pace,:max_number)');
$qry->bindParam(':user', $user);
$qry->bindParam(':country', $country);
$qry->bindParam(':city', $city);
$qry->bindParam(':location', $location);
$qry->bindParam(':distance', $distance);
$qry->bindParam(':pace', $pace);
$qry->bindParam(':max_number', $part);
$qry->execute();
if ($qry) { $message = "success"; }
else { $message = "failed"; }
echo utf8_encode($message);
?>
上面的代码有效,我可以在数据库中插入数据。
http://www.mywebsite.com/savedata.php?country=fakeCountry&location= fakeLocation&city=fakeCity&distance=fakeDistance&partecipant=fakePartecipant
先谢谢你的时间。
答案 0 :(得分:1)
插入数据的方式没有任何问题。我一直这样做。
将您的PHP文件视为一个非常简单的Web服务。
您应该做的是保护PHP脚本不被IOS应用程序中的过程以外的任何其他操作。你可以做一些事情。从您的应用程序传递带有某种标识的帖子参数,检查用户代理。我也总是传递一个应用版本号,这样你就可以确保你的脚本保持向后兼容。
好的做法是确保设置$ _POST变量。 确保您在IOS应用程序中发布的数据也不会失败。检查html实体等。
希望这有帮助。
答案 1 :(得分:0)
这是我开展项目的方法。
iOS代码
-(IBAction)addData {
NSString *strURL = [NSString stringWithFormat:@"http://www.website.com/post.php?name=%@&description=%@",nameTextField.text, descriptionField.text];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
NSLog(@"%@", strResult);
}
PHP代码
<?php
// Create connection
$servername = "localhost";
$username = "admin";
$password = "admin";
$dbname = "database";
$con=mysqli_connect("localhost","username","password","database");
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
echo "Nothing happened";
}else{
}
if (isset ($_GET["dishname"]))
$name = $_GET["name"];
else
$name = "Null";
if (isset ($_GET["description"]))
$description = $_GET["description"];
else
$description = "Null";
echo "name : ". $name;
echo "description : ". $description;
$sql = "insert into RecipeFeed (Name, Description) values ('".$name."','".$description."')";
$result = mysqli_query($con, $sql);
?>
希望有所帮助
答案 2 :(得分:0)
NSString *rawStr = [NSString stringWithFormat:@"country=%@&location=%@&&city=%@&distance=%@&pace=%@&partecipant=%@", country,
location,
city,
distance,
pace,max_part];
你有2&amp;在城市之前。