Swift Mysql PHP check database

I'm working on an app in xcode that allows users to enter information on a registration page. I'm very new to swift but I was wondering if it is possible for when the user is entering information in the username textfield, with each character the user types, it will call mysql database to check to see if the username is available or not. I am using php as the middle man between swift and mysql. However, I've struggled to get this code to work. Does anyone know if this is even possible? and if so, how can I correct my code to make it work? Thanks a lot in advance. My code is posted below:


@IBAction func TextFieldEditingDidChange(_ sender: Any) { 
let request = NSMutableURLRequest(url: NSURL(string: "usernamecheck.php")! as URL)
request.httpMethod = "POST" 
print("Request: \(request)")
let postString = "username=\(usernameTxt.text!)"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(error)")
return 
} // Ends errror If statements  
print("response = \(response)")
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) 
print("responseString = \(responseString)") 
} 
task.resume() 
}


portion PHP file:


$a_sql = mysqli_query($con, "SELECT username FROM users WHERE username = '" . mysqli_real_escape_string($_POST['username']) . "' ; ");
if (empty($_POST['username'])) { 
$username_error = "Please input username"; 
} else { 
$a_sql; 
} if ($a_sql && mysqli_num_rows($a_sql) > 0) { 
$username_exists = "Username is already taken."; 
echo $username_exists; 
} else { 
echo "Fail"; 
}

Accepted Reply

So you request is incorrect.


A correct form is :


$a_sql = "SELECT username FROM users WHERE username = '$form_nom'";


So in your case, why do you add ";" ?


I think it should be close to this (I'm not tatally sure with mysql syntax) :

$a_sql = "SELECT username FROM users WHERE username = 'mysqli_real_escape_string($_POST['username'])'";


or simply


$a_sql = "SELECT username FROM users WHERE username = '$_POST['username']'";


For test, also echo:

$_POST['username']

$a_sql

Replies

It is always responding saying "Fail" even though the username is already taken.

I suspect your sql request after username =


$a_sql = mysqli_query($con, "SELECT username FROM users WHERE username = '" . mysqli_real_escape_string($_POST['username']) . "' ; ");


you have a double quote following the single quote that I don't understand


Could you echo $a_sql to see what exact request is used ?

When I echo $a_sql; nothing is returned. If I remove the double qoutes following the single qoutes the code does not work at all and it says my page has an error.


Anothing weird thing about this code is that the only time it tells me that a username is taken is when the textfield is blank.

So you request is incorrect.


A correct form is :


$a_sql = "SELECT username FROM users WHERE username = '$form_nom'";


So in your case, why do you add ";" ?


I think it should be close to this (I'm not tatally sure with mysql syntax) :

$a_sql = "SELECT username FROM users WHERE username = 'mysqli_real_escape_string($_POST['username'])'";


or simply


$a_sql = "SELECT username FROM users WHERE username = '$_POST['username']'";


For test, also echo:

$_POST['username']

$a_sql

Thanks a lot! This worked!

How did you parse this once received?