Insert into mysql database

Asked

Viewed 872 times

0

I’m making a website which has a field of login, which when logging leads to the administrator sector being able to enter, delete and change items from the database. Already the registration page is not entering in the database. In fact, it is not performing any function that should perform. Should not allow the registration if the passwords did not hit, should not allow any field lacked, and if everything was right, should simply register and take the screen login.

Could you help me? Here are the codes:

<body>
    <div class="container container-twelve">
        <div class="four columns offset-by-four">
            <h1 class="titles">Cadastro</h1>
            <?php if(isset($_SESSION["success"])) {?>
            <p><?= $_SESSION["success"] ?></p>
        <?php }?>
        <?php unset($_SESSION["success"]); ?>
        </div>
        <div class="four columns offset-by-four" id ="login">
            <form action="cadastra_usuario.php"  method="post">
                <label for="nome">Nome</label>
                <input type="text" name="nome" placeholder="Digite seu nome">
                <label for="email">Email de usuário </label>
                <input type="text" name="email" placeholder="Seu email para login">
                <label for="senha">Senha</label>
                <input type="password" name="senha" placeholder="Sua senha">
                <label for="senha2">Repita sua senha</label>
                <input type="password" name="senha2" placeholder="Repita sua senha">
                <input type="submit" value="Cadastrar">
            </form>

            <p><a href="index.php"> << Voltar para o site</a></p>
            <p><a href="login.php"> Já tenho um cadastro >> </a></p>
        </div>
    </div>
</body>

cadastra_usuario.php:
<?php 
    include('conecta.php');
    include('functions.php');
    include('function_usuario.php');

    $senha = $_POST['senha'];
    $senha2 = $_POST['senha2'];

    $cadastra = cadastraUsuario($_POST['nome'], $_POST['email'], $_POST['senha']);

    if($senha != $senha2){
        $_SESSION["danger"] = "As senhas não conferem!";
        header("Location: cadastro.php");
    }
    if($cadastra == null){
        $_SESSION["danger"] = "Complete todos os campos!";
        header("Location: cadastro.php");
    } else {
        $_SESSION["success"] = "Usuário cadastrado com sucesso.";
        header("Location: login.php");
    }
?>

And the function:

function cadastraUsuario($conexao, $nome, $email, $senha){
        $query = "insert into usuarios (nome, email, senha) values ('{$nome}', '{$email}', '{$senha}')";
        return mysqli_query($conexao, $query);
    }

If you need the full codes, please ask me.

  • 2

    Looks like you should call the function cadastraUsuario() after validations and not before.

  • 1

    That’s right, I called them in the function instead of the file, and everything worked out. Thank you !

  • 2

    @Gabriel here does not edit the question title to "solved" when we find an answer. Here we have to mark an answer as the correct one or post a new answer and mark it as correct, read more here [tour] and here [Ask]

  • I edited the question because "phpmyadmin" is not a "database", read this to understand the differences: What is the difference between mysql and phpmyadmin?

1 answer

1

You are including the user and then validating the information.

As the friend said in the comments, call the function cadastraUsuario() after verification

<?php 
    include('conecta.php');
    include('functions.php');
    include('function_usuario.php');

    $senha = $_POST['senha'];
    $senha2 = $_POST['senha2'];

    if($senha != $senha2){
        $_SESSION["danger"] = "As senhas não conferem!";
        header("Location: cadastro.php");
    }

    $cadastra = cadastraUsuario($_POST['nome'], $_POST['email'], $_POST['senha']);

    if($cadastra == null){
        $_SESSION["danger"] = "Complete todos os campos!";
        header("Location: cadastro.php");
    } else {
        $_SESSION["success"] = "Usuário cadastrado com sucesso.";
        header("Location: login.php");
    }


?>

One thing I couldn’t help noticing is that you are leaving the database error validating your data (I conclude this because you used the return of the Insert to know if the record was included). Ok, but I don’t vary like this. If I can suggest it, do so:

<?php 
        include('conecta.php');
        include('functions.php');
        include('function_usuario.php');

        $senha = $_POST['senha'];
        $senha2 = $_POST['senha2'];
        $nome = $_POST['nome'];
        $email = $_POST['email'];         
        $validacaoOK = true;

        if($senha != $senha2){
            $_SESSION["danger"] = "As senhas não conferem!";
            header("Location: cadastro.php");
        }

        // Valide o resto dos dados sempre junto com **$validacaoOK**       

        if ($validacaoOK) {
          $cadastra = cadastraUsuario($nome, $email, $senha);

          if($cadastra == null){
            $_SESSION["danger"] = "Complete todos os campos!";
            header("Location: cadastro.php");
          } 
        }else {
            $_SESSION["success"] = "Usuário cadastrado com sucesso.";
            header("Location: login.php");
        }


    ?>

P.S.: I don’t program in PHP, but the way would be this ai.

Browser other questions tagged

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