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.
Looks like you should call the function
cadastraUsuario()
after validations and not before.– rray
That’s right, I called them in the function instead of the file, and everything worked out. Thank you !
– Gabriel
@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]
– gmsantos
I edited the question because "phpmyadmin" is not a "database", read this to understand the differences: What is the difference between mysql and phpmyadmin?
– Guilherme Nascimento