how to avoid duplicate email registration PHP 7 - MYSQL-PDO

0

First of all good day my dear ones, well I have a small problem when developing a registration system since I am using VMC with PHP7 , MYSQL .htaccess and PDO.

The problem is that I can not avoid the duplicates of email at the time of making the records in the DB by Views, when records twice the same email keeps me normal to the DB please be serious very kind to help me see where I'm making mistakes or what I'm missing, I'm still new to POO .

These are my codes:

My Controller:

<?php
 class Registro{


  public function RegistroController(){
  if(isset($_POST["saveRegistro"])){
    if(preg_match('/^[a-zA-Z0-9]+$/', $_POST["nombre"])&&
       preg_match('/^[a-zA-Z0-9]+$/', $_POST["email"])&&
       preg_match('/^[a-zA-Z0-9]+$/', $_POST["password"])){

      $data = array("nombre"=>$_POST["nombre"],
                    "email"=>$_POST["email"],
                    "password"=>$_POST["password"]);

     $resp = RegistroModel::saveRegistroModel($data, "users");

         if($resp == "ok"){

            echo'El usuario fue registrado';
             }

         }
        else{
          echo $resp;
         }
      }
  }

My Model:

   <?php
require_once "modelo/conn.php";

class RegistroModel{

public function saveRegistroModel($dataModel, $tabla){

$conn = Conex::conectar()->prepare("INSERT INTO $tabla (nombre, email, 
        password) VALUES (:nombre, :email, :password)");

$conn -> bindParam(":nombre", $dataModel["nombre"], PDO::PARAM_STR);
$conn -> bindParam(":email", $dataModel["email"], PDO::PARAM_STR);
$conn -> bindParam(":password", $dataModel["password"], PDO::PARAM_STR);

 if($conn->execute()){

        return "ok";
}

    else{

        return "error";
    }

    $conn->close();

}


public function buscarUsuarioDuplicado($dataModel, $tabla){

$conn = Conex::conectar()->prepare("SELECT email FROM $tabla WHERE 
             email = :email");

    $conn -> bindParam(":email", $dataModel["email"], PDO::PARAM_STR);
    if($conn -> execute()){

        return "ok";

    }

    else{

        return "error";
    }

}
}

My Views:

        <h1>Registrarse</h1>
    <form  method="post" enctype="multipart/form-data">    
        <input name="saveRegistro" type="hidden" value="ok">            
        <input type="text" name="nombre" placeholder="Nombre">
        <input type="email" name="email" placeholder="Email">
        <input type="password" name="password" placeholder="Contraseña"><br>

     <button id="enviar" type="submit"><span> Registrarme</span></button>
            <?php
                $saveRegistro = new Registro();
                $saveRegistro -> RegistroController();
            ?>
    </form> 
    
asked by Alex 29.01.2018 в 18:13
source

0 answers