Insert into mysql database

0

I'm doing a site that has a login field, which logs into the admin sector and can insert, delete, and change database items. The registration page is not entering the database. In fact, it is performing no function that it should perform. Should not allow registration if the passwords do not hit, should not allow in case any field was missing, and if everything was right, you should simply register and take the login screen.

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 complete codes, please ask me.

    
asked by anonymous 04.12.2014 / 00:25

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 could not help noticing is that you're letting the bank error validate your data (concludes this because you used the insert return to see if the record was included). Okay, but I do not vary like this. If I can suggest you, 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");
        }


    ?>

Q: I do not program in PHP, but the path would be that way.

    
04.12.2014 / 11:06