User registration system

0

I already have the system, but I do not know how to develop the errors. For example: Passwords do not match, user already exists, email already exists.

    <?php

include('../includes/config.php');

$usuario    = $_POST ["usuario"];   
$email  = $_POST ["email"];
$senha = MD5($_POST["senha"]);
$query = "INSERT INTO 'users' ( 'usuario' , 'email', 'senha', 'usuario_id' ) 
VALUES ('$usuario', '$email', '$senha', '')";

mysql_query($query);

echo "";

?> 

And a form:

    <form action="registrar.php" method="post">
    <input name="usuario" type="text" id="usuario"><br>
    <input name="email" type="text" id="email"><br>
    <input name="senha" type="password" id="senha">
    <input name="senha2" type="password" id="senha2">
    </form>

The system already works, already registers and everything, only the errors are missing. And I wanted to get a browser script with the typed error.

    
asked by anonymous 22.07.2015 / 06:53

1 answer

0

Well this is simpler than it sounds, just pass the errors to $ _SESSION, and present them in the registration area. you have to start your code with session_start();

<?php
session_start();
// balbalbal mais codigo qualquer aqui

?>
<?php
   if(isset($_SESSION['erros']))
    {
   foreach($_SESSION['erros'] as $erro)
    {
        echo "<h2>$erro</h2>";
    }
    }
?>
<form action="registrar.php" method="post">
    <input name="usuario" type="text" id="usuario"><br>
    <input name="email" type="text" id="email"><br>
    <input name="senha" type="password" id="senha">
    <input name="senha2" type="password" id="senha2">
    </form>

In the try-to-register zone

<?php
session_start();
$_SESSION['erros'] = "";
$erros = array();
$i = 0;
// Quando o utilizador tiver algum erro basta adicionar isto
$i++;
$erros[$i] = "Erro nao sei qual.";

// No fim da detencao dos erros basta meter isto
$_SESSION['erros'] = $erros;
header("Location: register.php"); // Nao sei se o ficheiro para registar e esse mas vou tentar adivinhar xD
    
22.07.2015 / 11:47