通过POST方法从iOS设备将数据插入远程mysql数据库 [英] Insert data into remote mysql database by POST method from iOS device

查看:145
本文介绍了通过POST方法从iOS设备将数据插入远程mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个值:


  • id

  • name

  • 电子邮件

我有三个 UIText 字段我可以给这些输入并将这些值保存到远程数据库中。
我使用 GET 方法来完成它。我没有问题。但是,如果我想用 POST 方法做同样的事情,那我该怎么做呢。我认为下面现有的代码会有一些变化。如果有人知道解决方案,请与我分享。非常感谢提前。
祝你有愉快的一天。 :)

I have three UIText field where i can give these input and save these values into a remote database. I use GET method to accomplish it. And i have no problem. But, if i want to do the same thing with POST method then how can i do that. I think there will be a little change in the current existing code below. Please share with me if any one know the solution. A lot of thanks in advance. Have a nice day. :)

iOS部分代码:

- (IBAction)saveButton:(id)sender
{
    NSString *strURL = [NSString stringWithFormat:@"http://mydomain.com/iOS/Tulon/phpFile.php?name=%@&email=%@", nameTextField.text, emailTextField.text];

    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
    NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
    NSLog(@"strResult %@", strResult);
    nameTextField.text = nil;
}

远程服务器代码部分:

<?php
/* ----------------------------- Code for Dabase ----------------------------- */
    // Database Properties
    $dbhost = 'mydomain.com';
    $dbuser = 'username';
    $dbpass = 'password';
    $db = 'dbName';
    $dbtable = 'user';

    // Connect Database
    $conn = mysql_connect($dbhost,$dbuser,$dbpass) or die (mysql_error());
    mysql_select_db($db, $conn) or die(mysql_error());
/* ----------------------------- Code for Dabase ----------------------------- */

    if (isset ($_GET["name"]) && isset ($_GET["email"]))
    {
        $name = $_GET["name"];
        $email = $_GET["email"];
    }
    else
    {
        $name = "Tulon";
        $email = "tulon@yahoo.com";
    }

    // Insert value into DB
    $sql = "INSERT INTO $dbtable (id, name, email) VALUES (NULL, '$name', '$email');";
    $res = mysql_query($sql,$conn) or die(mysql_error());

    mysql_close($conn);

    if ($res)
    {
        echo "success";
    }
    else
    {
        echo "faild";
    }
?>


推荐答案

您可以使用NSURLSessionDataTask函数将数据发布到PHP并使用JSON获得响应。

You can use the NSURLSessionDataTask function to post data up to PHP and get a response with JSON.

- (IBAction)saveButton:(id)sender
{

NSString *noteDataString = [NSString stringWithFormat:@"name=%@&email=%@", nameTextField.text, emailTextField.text];

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

NSURL * url = [NSURL URLWithString:@"http://mydomain.com/iOS/Tulon/phpFile.php"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[noteDataString dataUsingEncoding:NSUTF8StringEncoding]];

NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *dataRaw, NSURLResponse *header, NSError *error) {
NSDictionary *json = [NSJSONSerialization
                              JSONObjectWithData:dataRaw
                              options:kNilOptions error:&error];
NSString *status = json[@"status"];

if([status isEqual:@"1"]){
//Success

} else {
//Error

}
}];

[dataTask resume];
}

您可以使用以下代码处理PHP中的响应:

and you can handle the response in PHP with this code:

<?php
if (isset ($_POST["name"]) && isset ($_POST["email"])){
    $name = $_POST["name"];
    $email = $_POST["email"];
} else {
    $name = "Tulon";
    $email = "tulon@yahoo.com";
}

// Insert value into DB
$sql = "INSERT INTO $dbtable (name, email) VALUES ('$name', '$email');";
$res = mysql_query($sql,$conn) or die(mysql_error());

mysql_close($conn);

if($res) {          
$response = array('status' => '1');                 
} else {
die("Query failed");
}

echo json_encode($res);
exit();
?>

希望这会有所帮助

这篇关于通过POST方法从iOS设备将数据插入远程mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆