verify registered user PHP Mysqli

Asked

Viewed 3,644 times

0

Hello, I know almost nothing about Php and mysqli. I am trying to implement the user system for the site, the registration is already working but I can register with the same email as many times as you want. How I check if there is already the email registering in the BD?

So far I’ve only got this, Form "index.php" ;

config.php (connects to the database);

controleindex.php (sending the values to the BD table)

<?php
require_once("config.php");

$nome = $_POST['nome'];
$sobre = $_POST['sobre'];
$email = $_POST['email'];
$senha = $_POST['senha'];


$mysqli->query("Insert into usuarios (nome,sobre,email,senha) values ('".$nome."','".$sobre."','".$email."','".$senha."')")

?>

2 answers

7

Just check if there is already any user with the past email

$result = $mysqli->query("SELECT COUNT(*) FROM usuarios WHERE email = '{$email}'");
$row = $result->fetch_row();
if ($row[0] > 0) {
    echo "E-mail já cadastrado";
} else {
    $mysqli->query("Insert into usuarios (nome,sobre,email,senha) values ('".$nome."','".$sobre."','".$email."','".$senha."')");
}

You must prevent SQL injection into your code, the way it is written SQL can be easily injected, here has an explanation of how to prevent.

  • Wow, thank you so much!! just one more question, how can I put the message as a warning or something? I use $Row to do this?

  • 1

    Then it depends on the flow you will use, you can redirect the user to the same page of the registration setting in the session or passing through the url a value to inform that the email has already been registered. Ex: http://houseofcoin.com/pt/?msg=emailcadastro then you check whether the $_GET['msg'] was set and display the message according to this value. Or even (an elegant solution), give a alert kind of: "echo <script>alert('E-mail já cadastrado!')</script>" and redirect to the registration page.

  • 1

    @It’s oronaldodll just out of curiosity, I noticed that you accepted and dismissed this answer. There is something worth correcting/commenting on or missing in the answer?

  • @Sergio I’m just validating the answer that fits the most problem, for in the future other users can benefit from it.

  • @That’s why the choice is yours, always. I was just curious because others voted for her and had not left comment for the author to realize what failed or what may be missing.

  • @Sergio really, it was a mistake on my part...

Show 1 more comment

0


$result = $mysqli->query("SELECT COUNT(*) FROM usuarios WHERE email = '$email'");
$row = $result->fetch_row();
if ($row[0] > 0) {
    $alerta =("E-mail (".$email.") já existente.");
} else {
    $mysqli->query("Insert into usuarios (nome,sobre,email,senha) values ('".$nome."','".$sobre."','".$email."','".$senha."')");
    $alerta =("Cadastro realizado com sucesso!!");
}

if ($alerta != ""){
echo ("<script language=\"javascript\" src=\"http://SeuDominio.com/fancybox/jquery-1.4.3.min.js\"></script>

<script language=\"javascript\" src=\"http://SeuDominio.com/fancybox/jquery.fancybox-1.3.4.pack.js\"></script>

<link rel=\"stylesheet\" type=\"text/css\" href=\"http://SeuDominio.com/fancybox/jquery.fancybox-1.3.4.css\" 
media=\"screen\" />

<script type=\"text/javascript\">
jQuery(document).ready(function() {
        $.fancybox(
        '".$alerta."',
        {
        'autoDimensions'    : false,
        'width'             : '300',
        'height'            : '50',
        'transitionIn'      : 'none',
        'transitionOut'     : 'none'
        }
        );
    });
</script>");
}
  • unfortunately, it didn’t work... the code runs, but it doesn’t open the fancybox and below the "Else" that register has a "}" the most, that’s what I was wanting, but thanks for trying.

  • sent, but if you want to see how the form with the code you passed is the link http://houseofcoin.com/pt

  • If you want to show a message like this, @Éoronaldodll makes an ajax that the code is more beautiful and readable.

Browser other questions tagged

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