PHP function for checking information in the database

Asked

Viewed 449 times

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>';
}       
?>

1 answer

1

Another way for you to use:

It is currently recommended to use the PDO library as it is very secure, facilitate migration to other banks, and 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;  
        }
    }

}

Browser other questions tagged

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