Validation via web service

Asked

Viewed 53 times

0

I need to do a validation in my mysql database if the email you are trying to register already exists in the database, for this I have my 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 don’t know what I have to return if I fall into the first condition (there is the registered email). I have to show on android app that this email has already been registered.

1 answer

0

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

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

This way, you just do the treatment in your application the moment you receive the response from the server, reading the JSON data. Basically it would be this way:

JSONObject obj = new JSONObject(resposta);
String message = obj.getString("message");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.