I am pretty new to Swift development and have been following some tutorials to get this far with my API, I am just unsure as to why the second statement is not working with swift when it seems to be working fine in tests with POSTMAN. This works fine when testing on POSTMAN but only inserts into the logIn table, the second insert only works in postman and not in Swift when testing on an emulator here is the swift code
`let URL_USER_REGISTER = "URLString"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let date = formatter.string(from: dob.date)
print(date)
//send parameters
let parameters: Parameters=[
"username":usernameTextField.text!,
"password":passwordTextField.text!,
"trainer": trainer,
"first": firstNameTextField.text!,
"surname": surnameTextField.text!,
"email": emailTextField.text!,
"dob": date,
"goal": goalTextField.text!,
"weight": weightTextField.text!,
"height": heightTextField.text!]
//Sending http post request
let headers = [
"Content-Type": "application/x-www-form-urlencoded"
]
Alamofire.request(URL_USER_REGISTER, method: .post, parameters: parameters, encoding: URLEncoding()).responseJSON
{
response in
//printing response
print(response)
//getting the json value from the server
if let result = response.result.value {
// converting it as NSDictionary
let jsonData = result as! NSDictionary
// displaying the message to user
self.displayMessage(userMessage: (jsonData.value(forKey: "message") as! String?)!)
}
}
}`
and the php files
<?php
//importing required script
require_once '../includes/DbOperation.php';
$response = array();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!verifyRequiredParams(array('username', 'password', 'trainer', 'first', 'surname', 'email', 'dob', 'goal', 'weight', 'height'))) {
//getting values
$username = $_POST['username'];
$password = $_POST['password'];
$trainer = $_POST['trainer'];
$first = $_POST['first'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$dob = $_POST['dob'];
$goal = $_POST['goal'];
$weight = $_POST['weight'];
$height = $_POST['height'];
//creating db operation object
$db = new DbOperation();
//adding user to database
$result = $db->createUser($username, $password, $trainer, $first, $surname, $email, $dob, $goal, $weight, $height);
//making the response accordingly
if ($result == USER_CREATED) {
$response['error'] = false;
$response['message'] = 'Your account has been created';
} elseif ($result == USER_ALREADY_EXIST) {
$response['error'] = true;
$response['message'] = 'User already exist. Please try again';
} elseif ($result == USER_NOT_CREATED) {
$response['error'] = true;
$response['message'] = 'Sorry, your request could not be processed at this time.';
}elseif ($result == LOGIN_CREATED_USER_NOT_CREATED){
$response['error'] = true;
$response['message'] = 'LogIn Created, Profile could not be created.';
}
} else {
$response['error'] = true;
$response['message'] = 'Please enter all fields';
}
} else {
$response['error'] = true;
$response['message'] = 'Invalid request';
}
//function to validate the required parameter in request
function verifyRequiredParams($required_fields)
{
//Getting the request parameters
$request_params = $_REQUEST;
//Looping through all the parameters
foreach ($required_fields as $field) {
//if any requred parameter is missing
if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) {
//returning true;
return true;
}
}
return false;
}
echo json_encode($response);
public function getUserByUsername($username)
{
$stmt = $this->conn->prepare("SELECT id, username, trainer FROM LogIn WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->bind_result($id, $uname, $trainer);
$stmt->fetch();
$user = array();
$user['id'] = $id;
$user['username'] = $uname;
$user['trainer'] = $trainer;
return $user;
}
//Function to create a new user
public function createUser($username, $pass, $trainer, $firstName, $surname, $email, $dob, $goal, $weight, $height)
{
if (!$this->isUserExist($username)) {
$password = md5($pass);
$stmt = $this->conn->prepare("INSERT INTO LogIn (username, password, trainer) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $username, $password, $trainer);
if ($stmt->execute()) {
$user = $this->getUserByUsername($username);
//var_dump($user["id"]);
$stmt2 = $this->conn->prepare("INSERT INTO Client(client_id, client_first_name, client_surname, client_email, client_dob,
client_goal, client_weight, client_height, number_of_cancellations)VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0)");
$stmt2->bind_param("ssssssss", $user["id"], $firstName, $surname, $email, $dob, $goal, $weight, $height);
if ($stmt2->execute()){
return USER_CREATED;
}else {
return LOGIN_CREATED_USER_NOT_CREATED;
}
} else {
return USER_NOT_CREATED;
}
} else {
return USER_ALREADY_EXIST;
}
}
private function isUserExist($username)
{
$stmt = $this->conn->prepare("SELECT id FROM LogIn WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->store_result();
return $stmt->num_rows > 0;
}
}
I have tried looking around and cant seem to find an answer for what I am trying to do. The application is able to communicate fine with the API and in turn with the database as the first insert is working fine. I presume the error is something to do with the parameters being sent from the iOS application as it is working when tested in POSTMAN
Thank you in advance for any help you can give.