Function validate users registration

Asked

Viewed 129 times

0

I’m starting in OO in php, and with the function below I’m trying to check if the name coming from the form is equal to some value coming from my user table before making Insert in the table. I wish someone could shed some light since none of the attempts have worked so far and this is as close as I can get.

    public function validarDadosUsuario($validaNome) {
        require('conexao.php');
        $sql = "SELECT * FROM usuario";
        $result = $conn->query($sql);
        $dados = $result->fetchAll(PDO::FETCH_ASSOC);

        foreach ($dados as $row) {

            foreach ($row as $indice => $value) {

                if($row[$indice] == $validaNome) {
                    echo 'Nomes iguais';
                    $this->$validaNome = true;
                    break;

                } else {
                    echo 'Nomes diferentes';
                    $this->validaNome = false;
                }
            }

        }

        return $validaNome;
    }

$usuario = new Usuario($_POST['add_name'], $_POST['add_email'], $_POST['add_type'], $_POST['add_password']);

$user->validatedUsuario($_POST['add_name']);

  • only with this data we cannot help

1 answer

1

If the function is only to validate the Name, you could already select in the table using the value passed by $_POST['add_nome'] and from that it is only you do the treatment using if the select returns True or False.

Kind of like this:

$usuario = new Usuario($_POST['add_nome'], $_POST['add_email'], $_POST['add_tipo'], $_POST['add_senha']);
$usuario->validarDadosUsuario($_POST['add_nome']);

public function validarDadosUsuario($validaNome) {
        require('conexao.php');
        $sql = "SELECT * FROM usuario WHERE nomeDacoluna = '$validaNome'"; //A condição WHERE pra buscar os dados na tabela
        $result = $conn->query($sql);

        //Retorna o número de linhas do resultado do select
        $retorno = $result->rowCount();

        //Verificando se o select retornou algum resultado;
        if($retorno != 0):
            echo 'Já existe esse Nome (Nome Repetido)';
        exit;
        else:
            echo 'Não Existe esse Nome (Nome Novo)';
        exit;
        endif;
    }
  • Hello Jonatas good morning, thank you very much for the explanation, this is what I wanted to do, I had not thought to already bring from my consultation the data that I would like to compare. my question is the following, I can now inside the block if the name is different call my function to register the data in the database? (which is already working) and if it is the same as adding a die() and returning an error message?

  • Yes, inside the if you can put the code according to what you need to do. .

  • Hello Jonatas, I did the test and it worked, but I’m having problems with namspace in my registration code, I’m getting the following error when using my file that validates the data to register: Namespace declaration statement has to be the very first statement or after any declare call in the script. I already checked and the namespace statement is at the beginning of the script, please if you could take a look at my code I appreciate it, I haven’t found a solution yet. https://github.com/wildrove/projeto_biblioteca

  • In your validar_login.php file you have a blank line right at the beginning of the file before the '<? php' tag removes this space and runs the test. I think the reason for this mistake is precisely because of this. Remembering that if it works leaves a ok here for me to know if I could help you.

  • Hello Jonatas, actually I had removed only the space of the line where the namespace was, the line above the php tag I had not removed. Thank you!

Browser other questions tagged

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