Avoid more than one record in the database

5

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

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

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 = > link

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 have refactored the whole code and now it does not add more than one value, but it also does not warn the user that he already has the data:

<?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 you get this error:

  

Warning: mysql_num_rows (): supplied argument is not a valid MySQL result resource in /home/a1478344/public_html/analytics/register.php on line 40

    
asked by anonymous 22.11.2015 / 16:09

2 answers

3

I was able to solve it as follows:

Running the query in the database to see if the user and email already exist! otherwise 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 registry query you put the following:

uses $ exists = ExistUser (username, email); if it is false it executes a query by adding the user to the DB. if it is true it alerts you that it does not exist.

$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>";
            }
    
22.11.2015 / 17:45
1

If the code of your exite_ja_register () function is in another file, you need to make an include of it in register.php the same way you did with mysql-connector.php

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

 <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.1.js"></script><scripttype="text/javascript">
   $('#id_do_form').submit(function() {
   $('#img_carregando').show(); // mostra div com imagem CARREGANDO
   $('#botao_gravar').hide(); // oculta botao GRAVAR
   return true; 
  });
</script>
    
22.11.2015 / 16:51