How to post data from iOS app to MySQL database?

前端 未结 3 427
独厮守ぢ
独厮守ぢ 2021-01-24 23:11

I saw a similar post to my question but his solution did not work for me for some odd reason and it is making me age faster than Obama.

Basically I want to post data fro

相关标签:
3条回答
  • 2021-01-24 23:36

    As mapek already posted code for PHP , let me post answers for iOS part only. You can pass the parametres in POST like below.

    Method: 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);
    }];
    

    Method 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);
    }];
    
    0 讨论(0)
  • 2021-01-24 23:40

    I figured it out and here is the code below.

    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);
    
    0 讨论(0)
  • 2021-01-24 23:48

    You are passing your parameters via GET. So that you have to change

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

    to

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

    in your PHP script.

    0 讨论(0)
提交回复
热议问题