mat*_*mat 6 php mysql iphone pdo ios
我在这里和这里找到了一些信息, 但我没有找到关于此事的教程或好书.
我不想使用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
}
Run Code Online (Sandbox Code Playgroud)
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);
?>
Run Code Online (Sandbox Code Playgroud)
上面的代码工作,我能够在数据库中插入我的数据.
http://www.mywebsite.com/savedata.php?country=fakeCountry&location= fakeLocation&city=fakeCity&distance=fakeDistance&partecipant=fakePartecipant在此先感谢您的时间.
小智 1
您插入数据的方式没有任何问题。我一直这样做。
将您的 PHP 文件视为一个非常简单的 Web 服务。
不过,您应该做的是保护 PHP 脚本不被 IOS 应用程序中的过程以外的任何其他程序运行。您可以做一些事情。从您的应用程序传递带有某种标识的后参数,检查用户代理。我总是传递应用程序版本号,这样您就可以确保您的脚本保持向后兼容。
好的做法是确保设置了 $_POST 变量。确保您在 IOS 应用程序中发布的数据也不会失败。检查 html 实体等。
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
4131 次 |
| 最近记录: |