Validation via web service

0

I need to do a validation in my mysql database if the email they are trying to register already exists in the database, so I have the following code in the web service:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    require 'connect.php';
    inReg();
}   

function inReg() {

    global $connect;
    $email = $_POST["email"];
    $sql = "SELECT * FROM usuario WHERE email like '$email'";
    $resultado = mysqli_query($connect, $sql);

    if (mysqli_num_rows($resultado) <= 0) {
        $nomepasta = $_REQUEST['email'];
        $UploadFotos = "../../../webapps/ROOT/imagens/".$nomepasta;
        //$UploadFotos = "UploadFotos/".$nomepasta;

        if (!file_exists($UploadFotos)) {
            if (!mkdir($UploadFotos));

        }
        global $connect;

        $nome = $_POST["nome"];
        $senha = $_POST["senha"];
        $codificada = md5($senha);
        $email = $_POST["email"];
        $telefone = $_POST["telefone"];

        $query = " Insert into usuario(nome, senha, email, telefone) values ('$nome','$codificada','$email','$telefone')";

        mysqli_query($connect, $query) or die(mysqli_error($connect));
        mysqli_close($connect);

    } else { 
          ? ? ? ? ? ? ? ? ? ? ? ? ? ?
    }
} ?>

I do not know what to return if I fall into the first condition (there is an email address). I have to show in the android application that this email has already been registered.

    
asked by anonymous 27.05.2017 / 00:48

1 answer

0

You can simply return a JSON using echo with a specific message. Example:

} else {
    echo "{\"message\": \"Ja existe um usuario cadastrado com este email\"}";
}

In this way, you only have to make the treatment in your application the moment you receive the response from the server, reading the JSON data . Basically it would look like this:

JSONObject obj = new JSONObject(resposta);
String message = obj.getString("message");
    
27.05.2017 / 01:38