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
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".
– user28595
yes, and it was also html tag error, I put
</alert>nay</script>after I saw it!– FRNathan13
@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.
– FRNathan13
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.
– user28595