Empty data inserted into MYSQL database with iOS interface

I am creating an application that requires to insert a row into a mysql database (hosted on the Internet), however whenever I try to insert data, the data is empty. there is nothing wrong with php since my web interface works fine with php script.

NSString *user = self.registerUsername.text; NSString *password = self.registerPassword.text; NSString *email = self.registerEmail.text; NSString *model = [NSString stringWithFormat:@"%@", [[UIDevice currentDevice] model]]; NSString *sysVer = [NSString stringWithFormat:@"%@", [[UIDevice currentDevice] systemVersion]]; NSString *connectionURL = @"mydomain.com/Register.php"; NSLog(@"%@", connectionURL); //this is logged correctly NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:connectionURL]]; [request setHTTPMethod:@"POST"]; NSString *insert = [[NSString alloc] initWithFormat:@"username=%@&password=%@&email=%@&model=%@&sysver=%@", user, password, email, model, sysVer]; NSLog(@"%@", insert); [request setHTTPBody:[insert dataUsingEncoding:NSASCIIStringEncoding]]; //This is also logged correctly, giving me the right format i would expect NSURLResponse *response; NSError *error; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; NSString *responseString = [NSString stringWithUTF8String:[responseData bytes]]; NSLog(@"%@", responseString); //The response string is also correct, but no data is inserted into the database NSString *success = @"success"; [success dataUsingEncoding:NSUTF8StringEncoding]; NSLog(@"%lu", (unsigned long)responseString.length); NSLog(@"%lu", (unsigned long)success.length); 

mRegister.php

 <?php header('Content-type: text/plain; charset=utf-8'); include("config/dp.php"); //<- db connection $con = mysql_connect("127.0.0.1","user","pass"); if(! $con) { die('Connection Failed'.mysql_error()); } mysql_select_db("hk", $con); $message = ""; $username = ($_POST['username']); $password = ($_POST['password']); $email = ($_POST['email']); $model = ($_POST['model']); $sysver = ($_POST['sysver']); $submit = $_POST["registerform"]; $sql = "INSERT INTO members (`username`, `password`, `email`, `model`, `sysver`) VALUES ('$username', '$password', '$email', '$model', '$sysver')"; $result = mysql_query($sql); $row = mysql_fetch_array($result); if ($result) { $message = "success"; } else { $message = "failed"; } echo utf8_encode($message); ?> 

also, responseString usually appears with some strange characters after it affects the value of responseString.length . Is something wrong with my code or is it related to my php?

0
source share
1 answer

Perhaps PHP does not parse the request body because you did not specify the Content-Type request header:

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

On the other hand, I recommend you use the iOS HTTP library, such as AFNetworking , so you don’t have to worry about manually creating the request body. Check its documentation .

0
source

Source: https://habr.com/ru/post/957011/


All Articles