Registration is not being recorded in the bank

Asked

Viewed 66 times

-1

Hello, I am creating a login/registration system for a college job, as a basis I am using some internet tutorials. I was able to develop well, but now I’m stuck... My registration form is not recording the information at the bank, I can’t identify why.

php.

<?php
require_once 'CLASSES/usuarios.php';
$u = new Usuario;

?>

<!DOCTYPE html>
<html lang="pt-br">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="CSS/style.css">
    <title>Cadastro</title>
</head>

<body class="bg-light">

    <form method="POST">
        <div class="form-group corpo-form">
            <h1 class="text-center">CADASTRO</h1>
            <p class="text-center">Complete se cadastro para ter acesso a área restrita.</p>
            <input type="text" name="nome" class="rounded-pill" placeholder="Nome">
            <input type="email" name="email" class="rounded-pill" placeholder="E-mail">
            <input type="password" name="senha" class="rounded-pill" placeholder="Senha de 6 digitos" maxlength="6">
            <input type="password" name="confirmsenha" class="rounded-pill" placeholder="Confirmar Senha" maxlength="6">
            <input type="submit" class="btn btn-dark rounded-pill" value="Cadastrar ">
            <a href="index.php" id="alinhameno-link">← Voltar</a>
        </div>

        <?php
//verificar se clicou no botao
if (isset($_POST['nome'])){
    $nome =  $_POST['nome'];
    $email =  $_POST['email'];
    $senha =  $_POST['senha'];
    $confirmsenha = $_POST['confirmsenha'];
    //verificar se as informacoes estao completas
    if(!empty($nome) && !empty($email) && !empty($senha) && !empty($confirmsenha)){

        $u->conectar("dbi","localhost", "root","");
        if($u->msgErro =="")//sem erros
        {
            if($senha == $confirmsenha){
               if($u->cadastrar($nome, $email, $senha)){
                echo "Cadastrado com sucesso! Faça seu login para entrar.";
               }
               else{
                   echo "E-mail já cadastrado!";
               }
            }
            else{
                echo "As senhas não coincidem";
            }

        }
        else{
            echo "Erro: ".$u->msgErro; 
        }
    }
    else{
        echo "Preencher todos os campos!";}
    }
    ?>

    </form>

    </body>

</html>

php users.

<?php

class Usuario
{
    private $pdo; 
    public $msgErro = "";//sem erros

    public function conectar( $nome, $host, $usuario, $senha)
    {
        global $pdo;
        try {
            $pdo = new PDO ("mysql: dbname=".$nome.";host=".$host,$usuario,$senha);
        } catch (PDOException $e){
            $msgErro = $e->getMessage();

        }

    }

    public function cadastrar($nome, $email, $senha)
    {
        global $pdo;
        //verificar se já existe e-mal cadastrado
        $sql = $pdo->prepare("SELECT id_usuario FROM usuarios WHERE email = :e ");
        $sql->bindValue(":e",$email);
        $sql->execute();
        if($sql->rowCount() > 0){
            return false; // Já está cadastrado
        }
        //Caso não, cadastrar
        else{
            $sql = $pdo->prepare("INSERT INTO usuarios (nome,email,senha) VALUES(:n, :e, :s)");
            $sql->bindValue(":n",$nome);
            $sql->bindValue(":e",$email);
            $sql->bindValue(":s", md5($senha));
            $sql->execute();
            return true;
        } 

    }

    public function logar($email, $senha)
    {
        global $pdo;
        //verificar se email e senha estão cadastrados,se sim
        $sql = $pdo->prepare("SELECT id_usuario FROM usuarios WHERE email =:e AND senha = :s");
        $sql->bindValue(":e",$email);
        $sql->bindValue(":s",md5($senha));
        $sql->execute();
        if( $sql->rowCount() > 0){
            //entrar no sistema/sessao
            $dado = $sql->apc_fetch();
            session_start();
            $_SESSION['id_usuario'] = $dado['id_usuario'];
            return true; //login efetuado com sucesso
        }
        else{
            return false;//nao foi possivel logar
        }

    }
}

1 answer

0

From what I’ve seen of your code, you post the form data to the same page. Although the page calls your class "User", you are not opening the connection to the bank and calling the function "register".

One of the ways to solve this would be to change the code as follows:

<?php
require_once 'CLASSES/usuarios.php';
$u = new Usuario;
//Se existir um POST com estes 3 campos...
if (isset($_POST['nome']) && isset($_POST['email']) && isset($_POST['senha'])) {
	//Abrir conexão e executar função de gravar	
	$u->conectar("nome do banco, "nome do servidor", "usuario banco", "senha banco");
	$u->cadastrar($_POST['nome'], $_POST['email'], $_POST['senha']);
}
?>

  • Unfortunately still not recording in the bank, until appears the send confirmation but the bank remains empty.

Browser other questions tagged

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