Error checking fields in database

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 am trying to verify that there are equal fields (username and email) in the database, so that if it exists do not let register. Does anyone know where the error is? is that it inserts in the same even if the fields are the same

    
asked by anonymous 17.05.2015 / 04:50

1 answer

1

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

SELECT * FROM utilizadores WHERE username = ? and email = ?
    
17.05.2015 / 07:50