Error checking fields in database

Asked

Viewed 56 times

1

    //código da classe
    public function ver_utilizadores (){
    $bd = new ligacao_bd();   
    $ver_utilizador = "SELECT * FROM utilizadores";
    $resultado = $bd -> realizarQuery($ver_utilizador);
    return ($resultado);
    }
//codigo do script
if(!$erros){
        $username= $_POST['username'];
        $email= $_POST['email'];
        try{
        $verificar = new utilizadores();
        $resultado = $verificar -> ver_utilizadores($username, $email);
        if($resultado -> num_rows >= 1){
        echo "asdasd";
        }else{      
        $insere = new utilizadores();
        $resultado = $insere -> inserir_utilizador($username, $password,$email);
        header('location: login.php');
        }
    }//fim try
        catch (Exception $e){
            echo "Erro de inserção";    
        }

I’m trying to check if there are equal fields ( username and email ) in the database, so if it exists do not let register. Does anyone know where the mistake is? is that it inserts even if the fields are equal

  • I got it wrong, when you printed (echo) $resultado->num_rows what appears?

1 answer

1

Well, in the third line of your code in the variable $ver_utilizador = "SELECT * FROM utilizadores", no user information and email soon the call of your method ver_utilizadores($username, $email) always brings all records of the table, not making any validation if the user exists or not, put the clause "WHERE" in your SQL using the username and email fields, example:

SELECT * FROM utilizadores WHERE username = ? and email = ?

Browser other questions tagged

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