Function in PHP to verify information in the database

0

Hello, I need to check in the registration form, if the user name that was passed already exists in the database. I would like the help of how do I make a function in PHP or javascript that does this query, without using a framework.

The only way you can do this is:

<?php
require_once('../conecta.php'); 

$nomeProfessor=$_POST['tNomeProfessor'];
$usuarioProfessor=$_POST['tUsuarioProfessor'];
$matriculaProfessor=$_POST['tMatriculaProfessor'];
$emailProfessor=$_POST['tEmailProfessor'];
$senhaProfessor=$_POST['tSenhaProfessor'];
$redigitarProfessor=$_POST['tRedigitarProfessor'];

// Verify if the existing user name

$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 Database

mysqli_query($conexao,"INSERT INTO professor (nomeProfessor, usuarioProfessor, matriculaProfessor, emailProfessor, senhaProfessor, redigitarProfessor) VALUES ('$nomeProfessor', '$usuarioProfessor', '$matriculaProfessor', '$emailProfessor', '$senhaProfessor', '$redigitarProfessor')");

mysqli_close($conexao);

// Successfully sign up message

if (isset($_GET['cadastradoSucesso']) && ($val == true)) {

echo '<script type="text/javascript">
alert("Cadastrado com sucesso!");
location.href="cadastroProfessor.php";    
</script>';
}       
?>
    
asked by anonymous 06.05.2018 / 05:39

1 answer

1

Another way for you to use:

  

Currently, it is recommended to use the PDO library as being very secure, to facilitate migration to other banks, and to offer a concise API between them.

if (isset($_POST['tUsuarioProfessor'])) {

    $usuarioProfessor=$_POST['tUsuarioProfessor'];

    //verifica se a variavel é uma variável vazia
    if (empty($usuarioProfessor)) {
        echo "Campo usuario obrigatorio!";
    } else {
        //conexão 
        //require_once('../conecta.php');
        $pdo = new PDO('mysql:host=localhost;dbname=nome_DB', 'USUARIO', 'SENHA');
        $query = $pdo->prepare("SELECT * FROM professor WHERE usuarioProfessor = ?");
        $query->bindValue(1, $usuarioProfessor);


        $query->execute();

        $num = $query->rowCount();
        if ($num == 1) {
            echo  "Nome de usuário ".$usuarioProfessor." já existente!!!!";


        } else {

        $nomeProfessor=$_POST['tNomeProfessor'];
        $matriculaProfessor=$_POST['tMatriculaProfessor'];
        $emailProfessor=$_POST['tEmailProfessor'];
        $senhaProfessor=$_POST['tSenhaProfessor'];
        $redigitarProfessor=$_POST['tRedigitarProfessor'];

        $sql = $pdo->prepare("INSERT INTO professor (nomeProfessor, usuarioProfessor, matriculaProfessor, emailProfessor, senhaProfessor, redigitarProfessor) VALUES (?, ?, ?, ?, ?, ?)");
        $sql->bindParam(1, $nomeProfessor);
        $sql->bindParam(2, $usuarioProfessor);
        $sql->bindParam(3, $matriculaProfessor);
        $sql->bindParam(4, $emailProfessor);
        $sql->bindParam(5, $senhaProfessor);
        $sql->bindParam(6, $redigitarProfessor);
        $sql->execute();

            if(!$sql){
                echo 'erro na consulta: '. $db->errno .' - '. $pdo->error;
            }else{
                echo "Insert realizado com sucesso";
            }

        $pdo = null;  
        }
    }

}
    
06.05.2018 / 08:56