0

I wrote the following script in PHP in order to signup the user in a MySQL table.

<?php

$host = "127.0.0.1";
$user = "root";
$password = "";
$database = "login_db";

$connessione = new mysqli($host,$user,$password,$database);

if ($connessione === false) {

    die("errore connessione db " .$connessione->connection_error);

}
    @$staff = $_POST['staff_ID'];
    @$password = $_POST['password'];
    @$email = $_POST['email'];
    @$cpt = $_POST['isCPT'];
    @$name = $_POST['Name'];


    echo $staff;

$sql = "INSERT INTO users(staff_ID, password , email, isCPT, Name) VALUES (?,?,?,?,?)";
if ($statment = $connessione->prepare($sql)){

    $statment -> bind_param("sssss",$staff,$password,$email,$cpt,$name); 
    $statment -> execute();
    $json_array = [
        "successSignUp"=>true
    ];
    header('Content-type:application/json;charset=utf-8');
    $json = json_encode($json_array);

     if ($json === false) {
        $json = json_encode(["jsonError" => json_last_error_msg()]);
        if ($json === false) {
            // This should not happen, but we go all the way now:
            $json = '{"jsonError":"unknown"}';
        }
        http_response_code(500);
     } else {
        echo $json;
     }
} else {
    echo "errore connessione";
}
$statment -> close();
$connessione -> close();

?>

using xcode i want to signin the user but i cant understand why i can't print out the json i received from the server .

for testing in the code below i'm try with Alamofire and swiftyjson to get the JSON from the server.

what i'm doing wrong ? is my php file or my alamorequest is wrong?

 
let parm : [String: Any] = ["staff_ID": staffID,"password":password, "email":email, "isCPT":isCpt, "Name": nameUser]
AF.request(self.linkServerDB, method: .post, parameters: parm).validate()
.responseJSON{ response in
 print(response)
 let json = JSON(response.result)
 print(json)
 }
 .responseData { dt in
    do {
   let json = try JSON(data: dt.data!)
   print(json)
   }catch {
   print("error")
  }
 }
Damiano Miazzi
  • 1,514
  • 1
  • 16
  • 38
  • Remove all `@` in front of your variable assignments and add proper validation instead. Those suppresses errors, making it much harder to debug issues like the one you're asking about. Specially since you ignore the issue completely and continue your script either way, just assuming all values exists and are valid. – M. Eriksson May 30 '21 at 07:46
  • Btw, you should never store passwords in plain text. Always create a secure hash of the passwords using [password_hash()](https://www.php.net/manual/en/function.password-hash.php) and only save that. Then you use [password_verify()](https://www.php.net/manual/en/function.password-verify.php) to compare a password against the hash when authenticating a user. – M. Eriksson May 30 '21 at 07:49
  • You're also inconsistent with what you output. Sometimes you output json, and sometimes you output text straight off. And you always `echo $staff;` before any other output (including headers, which _must_ needs to be set _before any_ output, or you'll run into the [headers already sent](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) issue) which will always render any json output invalid. You need to check your logic. There are plenty of issues here. – M. Eriksson May 30 '21 at 07:53

0 Answers0