When registering user, I need to check in the database if this user already exists and present error message

Asked

Viewed 95 times

0

It follows below my code. It does everything, but hangs on the "user already registered" message even if I insert a different user into the field!

Remarks:

$con is the connection to the database that is correct;

login is the id in the page field where you enter the data;

mysqli use;

the table is "users"

$login = $_POST['login'];
$senha = $_POST['senha'];

#Recolhendo os dados do formulário
$loginrecolha = mysqli_real_escape_string($con, $login);
$senharecolha = mysqli_real_escape_string($con, $senha);

# Verificando apenas um campo, no caso login.
$sql = $con->query("SELECT login FROM usuarios WHERE login='$loginrecolha'");
if(mysqli_num_rows($sql) > 0){
echo "Este usuário já existe";
exit();
} else {
 if(!$con->query("INSERT INTO usuarios (login,senha) VALUES 
('$login','$senha')")) die ('Os dados não foram inseridos');
 echo "Dados inseridos com sucesso";
}
  • How so hangs on the message "user already exists"? It means it always falls in the IF?

  • this friend... if I register any user existing in the table or does not fall in the if

  • If you take the instruction you’re in $sql and run in the bank, it finds some record ?

  • yes finds 1 record which is what you have

1 answer

0

Try changing the code this way:

$login = $_POST['login'];
$senha = $_POST['senha'];

#Recolhendo os dados do formulário
$loginrecolha = mysqli_real_escape_string($con, $login);
$senharecolha = mysqli_real_escape_string($con, $senha);

# Verificando apenas um campo, no caso login.
$sql = $con->query("SELECT login FROM usuarios WHERE login='".$loginrecolha."'");
if(mysqli_num_rows($sql) > 0){
echo "Este usuário já existe";
exit();
} else {
 if(!$con->query("INSERT INTO usuarios (login,senha) VALUES 
('$login','$senha')")
   echo "Dados inseridos com sucesso";
 else
   die ('Os dados não foram inseridos')) 
}
  • opa, thanks friend but keeps falling in the "user already exists" even inserting one that does not exist... and the last if changed as requested gave this error Parse error: syntax error, Unexpected 'echo' (T_ECHO) in....

Browser other questions tagged

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