0
Hello, I need to check the fomulário register, if the name of the user that was passed already exists in the database. I would like the help of how to make a Function in PHP or javascript that makes this query, without using a framework.
The only way I could do it was this:
<?php
require_once('../conecta.php');
$nomeProfessor=$_POST['tNomeProfessor'];
$usuarioProfessor=$_POST['tUsuarioProfessor'];
$matriculaProfessor=$_POST['tMatriculaProfessor'];
$emailProfessor=$_POST['tEmailProfessor'];
$senhaProfessor=$_POST['tSenhaProfessor'];
$redigitarProfessor=$_POST['tRedigitarProfessor'];
// Check if the existing username
$val=true;
$consulta = mysqli_query($conexao,"SELECT * FROM professor WHERE usuarioProfessor='$usuarioProfessor'");
$linha = mysqli_num_rows($consulta);
if($linha >= 1){
do{
$val = false;
echo '<script type="text/javascript">
alert("Nome de usuário já existente!");
location.href="javascript:history.go(-1)";
</script>';
} while ($val == true);
}
// Insert into the Database
mysqli_query($conexao,"INSERT INTO professor (nomeProfessor, usuarioProfessor, matriculaProfessor, emailProfessor, senhaProfessor, redigitarProfessor) VALUES ('$nomeProfessor', '$usuarioProfessor', '$matriculaProfessor', '$emailProfessor', '$senhaProfessor', '$redigitarProfessor')");
mysqli_close($conexao);
// Message from registered successfully
if (isset($_GET['cadastradoSucesso']) && ($val == true)) {
echo '<script type="text/javascript">
alert("Cadastrado com sucesso!");
location.href="cadastroProfessor.php";
</script>';
}
?>
Possible duplicate of Check if user name is unique in real time
– Costamilam
Please read this post https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079
– user60252