4 Replies
      Latest reply: Nov 18, 2016 6:59 PM by linkuanlu RSS
      Vladimir Level 1 Level 1 (0 points)

        I'm creating an iOS App that stores data to an online database using a PHP file and MySQL database.

         

         

        I managed to make the communication between the iOS app, the PHP file and finally to the database, but no data is written on it, I only know that the communication has been done because the ID field is incremented every time I press the SEND button on the App.

         

         

        What can be wrong with my code?

         

         

        Here is my iOS code:

         

         

            NSString *post = [NSString stringWithFormat:@"nome=%@&endereco=%@&telefone=%@&email=%@&mensagem=%@&coordenadas=%@", nome.text, adressLabel.text, telefone.text, email.text, mensagem.text, coordenadas.text];

            const char *urlUTF8 = [post UTF8String];

            NSString *postBody = [NSString stringWithUTF8String:urlUTF8];

          

            NSData *postData = [postBody dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

          

            NSString *postDataLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

          

            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

          

            [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://xxxxxxx.com/signup.php"]]];

            [request setHTTPMethod:@"POST"];

            [request setValue:postDataLength forHTTPHeaderField:@"Content-Length"];

            [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

            [request setHTTPBody:postData];

          

            NSURLResponse *response;

            NSError *err;

            NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];

            NSString *content = [NSString stringWithUTF8String:[returnData bytes]];

            NSLog(@"****** RESPOSTA: %@", content);

         

         

         

         

        And the PHP file:

         

         

            <?php

            $con=mysqli_connect("SERVER","USER","PASS","DATABASE");

            if (mysqli_connect_errno($con))

            {

               echo '{"query_result":"ERROR"}';

            }

         

            $nome = $_GET['nome'];

            $endereco = $_GET['endereco'];

            $telefone = $_GET['telefone'];

            $email = $_GET['email'];

            $mensagem = $_GET['mensagem'];

            $coordenadas = $_GET['coordenadas'];

         

            $result = mysqli_query($con,"INSERT INTO user (nome, endereco, telefone, email, mensagem, coordenadas)

                  VALUES ('$nome', '$endereco', '$telefone', '$email', '$mensagem', '$coordenadas')");

         

            if($result == true) {

            echo '{"query_result":"SUCCESS"}';

            }

            else{

                echo '{"query_result":"FAILURE"}';

            }

            mysqli_close($con);

            ?>

        • Re: iOS, PHP, MySQL - No data is being written to database
          eskimo Apple Staff Apple Staff (7,450 points)

          In situations like this the first step is deciding whether the problem is on the client side or the server side.  You should do this by looking at the request on the ‘wire’.  Is it as you expect it to be?  If so, you have to debug this on the server side.  If not, you can continue your investigation on the client.

          To look at the request on the wire use one of the tools referenced by QA1176 Getting a Packet Trace.  Given that you’re using HTTPS, one of the debugging proxies is going to be the best option.

          You should also take a look at Debugging HTTP Server-Side Errors, where I discuss these ideas in more depth.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

          • Re: iOS, PHP, MySQL - No data is being written to database
            Claude31 Level 5 Level 5 (1,885 points)

            It's been a long time I've not done this, but I had more information in the body.

             

            Typically, I formatted the post String like this :

             

                            for (key, value) in parameters! {

                                theBody.appendString("--\(boundary)\r\n")

                                theBody.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")

                                theBody.appendString("\(value)\r\n")

                            }

             

            boundary is a sequence for separation, key containe "nome", etc and value nome.text

            • Re: iOS, PHP, MySQL - No data is being written to database
              linkuanlu Level 1 Level 1 (0 points)

              your ios code use POST method  ,then your php file use GET method,is right?

                • Re: iOS, PHP, MySQL - No data is being written to database
                  linkuanlu Level 1 Level 1 (0 points)

                    like this:

                   

                    $nome = trim($_POST['nome']);

                      $endereco = trim($_POST['endereco']);

                      $telefone = trim($_POST['telefone']);

                      $email = trim($_POST['email']);

                      $mensagem = trim($_POST['mensagem']);

                      $coordenadas =trim($_POST['coordenadas']);

                   

                      $result = mysqli_query($con,"INSERT INTO user (nome, endereco, telefone, email, mensagem, coordenadas)

                            VALUES ('".$nome."', '".$endereco."', '".$telefone."', '".$email."', '".$mensagem."', '".$coordenadas."')")