Php 如何将iOS应用程序中的数据发布到MySQL数据库?

Php 如何将iOS应用程序中的数据发布到MySQL数据库?,php,ios,mysql,Php,Ios,Mysql,我看到了一篇与我的问题类似的帖子,但他的解决方案由于一些奇怪的原因对我不起作用,这让我比奥巴马老得更快 基本上,我想将iOS应用程序中的数据发布到MySQL数据库 iOS代码 NSString *strURL = [NSString stringWithFormat:@"http://www.example.com/phpfile.php?dishname=%@&description=%@",textfieldTwo.text, textfieldTwo.text]; NSData *

我看到了一篇与我的问题类似的帖子,但他的解决方案由于一些奇怪的原因对我不起作用,这让我比奥巴马老得更快

基本上,我想将iOS应用程序中的数据发布到MySQL数据库

iOS代码

NSString *strURL = [NSString stringWithFormat:@"http://www.example.com/phpfile.php?dishname=%@&description=%@",textfieldTwo.text, textfieldTwo.text];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResults = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@", strResults);
PHP代码

<?php

$servername="localhost";
$username="admin";
$password="admin";
$dbname="dbname";
$conn=mysqli_connect($servername, $username, $password, $dbname);
if (!$conn)
{
    die("Connection failed: " . mysqli_connect_error());
}
$dishname=$_POST['dishname'];
$description=$_POST['description'];
echo "Name : " . $dishname;
//echo "Mail : ". $mail;
$sql="insert into RecipeFeed (DishName, Description) values ('" . $dishname . "', '" . $description . "')";
$result=mysqli_query($conn, $sql);


?>

您正在通过GET传递参数。所以你必须改变

$dishname = $_POST['dishname'];
$description = $_POST['description'];


在您的PHP脚本中。

由于mapek已经发布了PHP代码,让我只发布iOS部分的答案。您可以在POST中传递参数,如下所示

方法:1

NSData*  submitData    = [[NSString stringWithFormat:@"dishname=%@&description=%@",textfieldOne.text, textfieldTwo.text] dataUsingEncoding:NSUTF8StringEncoding];
       NSMutableURLRequest *submitrequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.example.com/phpfile.php"]];
    NSString *request = [[NSString alloc]initWithData:submitData encoding:NSUTF8StringEncoding];
    NSLog(@"request is %@",request);
    [submitrequest setHTTPMethod:@"POST"];
    [submitrequest setHTTPBody:submitData];
 [NSURLConnection sendAsynchronousRequest:submitrequest
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
  {
  NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  NSLog(@"jsonString values=%@",jsonString);
  id values = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"json values=%@",values);
}];
方法2

 NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary];
 [dictionnary setObject:textfieldOne.text forKey:@"dishname"];
 [dictionnary setObject:textfieldTwo.text forKey:@"description"];
 NSError *error = nil;
 NSData *submitData = [NSJSONSerialization dataWithJSONObject:dictionnary
 options:kNilOptions
 error:&error];   
 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.example.com/phpfile.php"]];
 [request setHTTPMethod:@"POST"];
 [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
 [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
 [request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
 [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]]  forHTTPHeaderField:@"Content-Length"];
 [request setHTTPBody:jsonData]; 
 [NSURLConnection sendAsynchronousRequest:submitrequest
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
   NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  NSLog(@"jsonString values=%@",jsonString);
  id values = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"json values=%@",values);
}];

我算出了,下面是代码

PHP


$dishname
$description
@SibaPrasadHota之后,您的sql语句行中也缺少
。不幸的是,我真的认为这与我的PHP文件有关。正如我所说的,回显PHP文件中的每一行并查看Xcode的控制台,尝试我编写的两种方法。。“你一定会成功的。”SibaPrasadHota好的,我想我知道怎么回事了。。它与我的web服务器有关。当我尝试在我的web浏览器中打开并输入链接时,它不会读取php文件,而是显示我的默认页面。我非常确信您编写的代码和提供的PHP代码都能正常工作,我只需检查我的web服务器。@SibaPrasadHota我将继续使用它,我将回到这个问题并更新它。谢谢大家的帮助!我也试过了,但没有成功。。。我确信我的连接是正确的,因为我在其他PHP代码中使用了相同的连接来检索数据。这可能是我的连接,但不可能,因为我使用了与检索数据相同的连接。它没有插入任何内容:(json值返回nullNow,我添加了json字符串,因此现在您可以在任何地方进行回显,以便在NSLog中看到输出,并在此处发布响应。
 NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary];
 [dictionnary setObject:textfieldOne.text forKey:@"dishname"];
 [dictionnary setObject:textfieldTwo.text forKey:@"description"];
 NSError *error = nil;
 NSData *submitData = [NSJSONSerialization dataWithJSONObject:dictionnary
 options:kNilOptions
 error:&error];   
 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.example.com/phpfile.php"]];
 [request setHTTPMethod:@"POST"];
 [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
 [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
 [request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
 [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]]  forHTTPHeaderField:@"Content-Length"];
 [request setHTTPBody:jsonData]; 
 [NSURLConnection sendAsynchronousRequest:submitrequest
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
   NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  NSLog(@"jsonString values=%@",jsonString);
  id values = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"json values=%@",values);
}];
<?php

// Create connection
$servername = "localhost";
$username = "admin";
$password = "admin";
$dbname = "db";
$con=mysqli_connect("localhost","admin","admin","db");

if (!$con) {
 die("Connection failed: " . mysqli_connect_error());
 echo "Nothing happened";

}else{


}


if (isset ($_GET["firstname"]))
        $firstname = $_GET["firstname"];
    else
        $firstname = "Null";
if (isset ($_GET["lastname"]))
        $lastname = $_GET["lastname"];
    else
        $lastname = "Null";

if (isset ($_GET["email"]))
        $email = $_GET["email"];
    else
        $email = "Null";

if (isset ($_GET["password"]))
        $password = $_GET["password"];
    else
        $password = "Null";

if (isset ($_GET["timestamp"]))
        $timestamp = $_GET["timestamp"];
    else
        $timestamp = "Null";
$id = "null";


echo "FirstName : ". $firstname;
echo "LastName : ". $lastname;
$sql = "insert into Users (FirstName, ID, LastName, Email, Password, TimeStamp) values ('".$firstname."', '".$id."', '".$lastname."', '".$email."', '".$password."', '".$timestamp."')";
$result = mysqli_query($con, $sql);
?>
  NSString *strURL = [NSString stringWithFormat:@"http://www.example.com/register.php?firstname=%@&lastname=%@&password=%@&email=%@&",firstName.text, lastName.text, password.text, email.text];
    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
    NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
    NSLog(@"%@", strResult);