Avoid more than one register in the database

Asked

Viewed 879 times

5

I’m having problems because I’m making a community system where the user registers and shares stories.

More when I went to test clicked on register 2 times and 2 times it went to the database even with a function checking.

MYSQL:
CAMPO    |  TIPO
ID       |  INT (AutoIncrement)
USERNAME |  VARCHAR(12)
PASSWORD |  VARCHAR(12)
EMAIL    |  TEXT

When registering it executes the following code:

<?php
    include ('mysql_connector.php');

    $username = "";
    $email = "";
    $password = "";

    if(!empty($_POST)){

        if(!empty($_POST['username']) && !empty($_POST['email']) && !empty($_POST['password']) ){

            $msg = "";

            try{

                $username = $_POST['username'];
                $email = $_POST['email'];
                $password = $_POST['password'];

                if(!exite_ja_registrado()){
                    $cmd = "INSERT INTO `users`(`id`, `username`, `email`,`password`) VALUES(NULL, '$username', '$email', '$password')";
                    $result = mysql_query($cmd);
                    if(!$result){
                        echo $error = mysql_error($result);
                        echo "<script>alert('$error');</alert>";
                    }

                    else{
                        echo "<script>alert('Usuario registrado! Faça login agora!');</script>";
                    }
                }

                else if (exite_ja_registrado()){
                    echo "<script>alert('Já exite um usuário registrado com os mesmo dados! Faça login...');</alert>";
                }
            }

            catch (Exception $e){
                echo "<script>alert('Usuario, Email e Senha ja registrados!')</script>";
            }
        }
    }


?>
<html>
<body>
<?php include('google-analytics.php'); ?>
<center>
<form method="POST">
    <input type="text" placeHolder="Email" name="email" /><br>
    <input type="text" placeHolder="usuário" name="username" /><br>
    <input type="password" placeHolder="Senha" name="password" /><br>
    <input type="submit" value="Registrar" />
</form>
</center>
</body>
</html>

But even after pressing numerous times it puts the data back into mysql.

Take the test => http://brasilr2.net76.net/analytics/register.php

function exite_ja_registrado(){
            $cmd = "SELECT * FROM `users` WHERE `email`='$email' AND `password`='$password' AND `username`='$username'";
            $result = mysql_query($cmd);
            if(mysql_num_rows($result) == 1){ return true; }
            else { return false; }
        }

I redid the entire code and now it doesn’t add more than one value, but it also doesn’t warn the user that they already have the data registered:

<?php
    include ('mysql_connector.php');

    $username = "";
    $email = "";
    $password = "";

    if($_SERVER['REQUEST_METHOD'] == "POST" && !empty($_POST)){

        if(!empty($_POST['username']) && !empty($_POST['email']) && !empty($_POST['password']) ){

            $username = $_POST['username'];
            $email = $_POST['email'];
            $password = $_POST['password'];

            $existe = ExistUser($username, $email);

            if(!$existe){
                $cmd = "INSERT INTO `users`(`id`, `username`, `email`,`password`) VALUES(NULL, '$username', '$email', '$password')";
                $result = mysql_query($cmd);
                if(!$result){
                    echo "<script>alert('Já exite um usuário registrado com os mesmo dados! Faça login...');</alert>";
                }

                else{
                    echo "<script>alert('Usuario registrado! Faça login agora!');</script>";
                }
            }

            else{
                echo "<script>alert('Já exite um usuário registrado com os mesmo dados! Faça login...');</script>";
            }
        }
    }

    function ExistUser($u, $e){

        $cmd = "SELECT * FROM `users` (`email`, `username`) WHERE `username`='$u' AND `email`='$e'";
        $result = mysql_query($cmd);
        $rows = mysql_num_rows($result);

        if(1 == $rows){
            return true;
        }

        else{
            return false;
        }
    }
?>
<html>
<head>
<title>Registre-se para continuar...</title>
</head>
<body>
<?php include('google-analytics.php'); ?>
<center>
<form method="POST">
    <input type="text" placeHolder="Email" name="email" /><br>
    <input type="text" placeHolder="usuário" name="username" /><br>
    <input type="password" placeHolder="Senha" name="password" /><br>
    <input type="submit" value="Registrar" />
</form>
</center>
</body>
</html>

And now make that mistake:

Warning: mysql_num_rows(): supplied argument is not a Valid Mysql result Resource in /home/a1478344/public_html/Analytics/Register.php on line 40

2 answers

3


I was able to solve it this way:

Running the query in the database to see if the user and email already exist! if yes returns true. if it does not return false.

function ExisteUsuario($u, $e){

        $cmd = "SELECT * FROM `users` WHERE `username`='$u' AND `email`='$e'";
        $result = mysql_query($cmd);
        $rows = mysql_num_rows($result);

        if(1 == $rows){
            return true;
        }

        else{
            return false;
        }
    }

And in the consultation of the register the following is placed:

uses $existe = Existeusuario(username, email); if false, it executes a query by adding the user to the DB. if true he warns that there is no.

$existe = ExisteUsuario($username, $email);

            if(!$existe){
                $cmd = "INSERT INTO `users`(`id`, `username`, `email`,`password`) VALUES(NULL, '$username', '$email', '$password')";
                $result = mysql_query($cmd);
                if(!$result){
                    echo "<script>alert('Já exite um usuário registrado com os mesmo dados! Faça login...');</script>";
                }

                else{
                    echo "<script>alert('Usuario registrado! Faça login agora!');</script>";
                }
            }

            else if($existe){
                echo "<script>alert('Já exite um usuário registrado com os mesmo dados! Faça login...');</script>";
            }
  • I see you got it. That error you showed, probably the query was returning false, and the function can’t count lines of a "false".

  • yes, and it was also html tag error, I put </alert> nay </script> after I saw it!

  • @Diegofelipe kind of made a website for a gaming club, and people asked. so I worried about the query selecting both the email, both the username.

  • 1

    Right. Then mark your answer as accepted, I believe it will only be released in 24 hours, but it is useful if someone has the same problem.

1

If your function code exite_ja_registered() is in another file, you need to include it in Register.php the same way you did mysql-Connector.php

To hide the button you can make a call in jquery to hide the button

 <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.1.js"></script>    
 <script type="text/javascript">
   $('#id_do_form').submit(function() {
   $('#img_carregando').show(); // mostra div com imagem CARREGANDO
   $('#botao_gravar').hide(); // oculta botao GRAVAR
   return true; 
  });
</script>
  • It is not a matter of disabling the button! but when you click the button 2 times to send it sends 2 same results, only changing the ID

  • 1

    I suggest you cooloque the fields username and email as Unique in the database so the bank itself will reject recordings of these two equal fields

Browser other questions tagged

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