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