簡體   English   中英

如何將數據從iOS應用發布到MySQL數據庫?

[英]How to post data from iOS app to MySQL database?

我看到了與我的問題類似的帖子,但是由於某種奇怪的原因,他的解決方案對我不起作用,這使我的年齡比奧巴馬快。

基本上,我想將數據從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'];

$dishname = $_GET['dishname'];
$description = $_GET['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);
}];

方法二

 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

<?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);
?>

iOS版

  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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM