How to verify that the user that I intend to register already exists?

4

Here is the save process:

$nuevoobjeto= new Objeto();

$cedula = $_POST['cedula'];
$nombres = $_POST['nombres'];
$apellidos = $_POST['apellidos'];
$telefono = $_POST['telefono'];
$telefono1 = $_POST['telefono1'];
$telefono2 = $_POST['telefono2'];
$direccion = $_POST['direccion'];
$municipio = $_POST['municipio'];
$parroquia = $_POST['parroquia'];
$vulnerabilidad = $_POST['vulnerabilidad'];
$grupo= $_POST['grupo'];
$fecha_ingreso = $_POST['fecha_ingreso'];
$fecha_donacion = $_POST['fecha_donacion'];
$formacion_psico = $_POST['formacion_psico'];
$fecha_psico = $_POST['fecha_psico'];
$formacion_plan = $_POST['formacion_plan'];
$fecha_plan = $_POST['fecha_plan'];
$observacion = $_POST['observacion'];
$indicador_psico = $_POST['indicador_psico'];
$suspendida = $_POST['suspendida'];




    $foto = addslashes(file_get_contents($_FILES['foto']['tmp_name']));


    // $consulta=$nuevoobjeto->lista($cedula);
    // if ($cedula) {
    //  echo "<script> alert('usuario ya existe') + window.open('?accion=registrar','_self');</script>";
    // }else{



$nuevo=$nuevoobjeto->insertar($cedula, $nombres, $apellidos, $telefono, $telefono1, $telefono2, $direccion, $municipio, $parroquia, $vulnerabilidad, $grupo, $fecha_ingreso, $fecha_donacion, $formacion_psico, $fecha_psico, $formacion_plan, $fecha_plan, $observacion, $indicador_psico, $suspendida, $foto);

    // }        

 ?>

And here is the sentence that ends up saving the data to my table:

public function insertar($cedula, $nombres, $apellidos, $telefono, $telefono1, $telefono2, $direccion, $municipio, $parroquia, $vulnerabilidad, $grupo, $fecha_ingreso, $fecha_donacion, $formacion_psico, $fecha_psico, $formacion_plan, $fecha_plan, $observacion, $indicador_psico, $suspendida, $foto)
    {
        $sql=$this->con->prepare("INSERT INTO 'perfiles' (cedula, nombres, apellidos, telefono, telefono1, telefono2, direccion, municipio, parroquia, vulnerabilidad, grupo, fecha_ingreso, fecha_donacion, formacion_psico, fecha_psico, formacion_plan, fecha_plan, observacion, indicador_psico, suspendida, foto) VALUES ('$cedula', '$nombres', '$apellidos', '$telefono', '$telefono1', '$telefono2', '$direccion', '$municipio', '$parroquia', '$vulnerabilidad', '$grupo', '$fecha_ingreso', '$fecha_donacion', '$formacion_psico', '$fecha_psico', '$formacion_plan', '$fecha_plan', '$observacion', '$indicador_psico', '$suspendida', '$foto')"); 
        $sql->execute();
        if($sql){
        echo "<script> alert('Se ha Guardado con EXITO') + window.open('?accion=registrar','_self');</script>";

            }

They will already see the system doing almost everything I want but I need the system to show me that the user I am trying to register already exists in the database.

    
asked by Douglas Perez Colombo 10.04.2018 в 16:37
source

3 answers

4

I could try it in the following way:

$a = $db->query("INSERT INTO nombres (nombre, apellido, cedula) 
    (SELECT * FROM (SELECT '$nombre', '$apellido', '$cedula') AS tmp WHERE NOT EXISTS (SELECT cedula FROM nombres WHERE cedula = '$cedula') 
    LIMIT 1)")

This inserts the data whenever they do not already exist in the table, of course if you make a var_dump () this will always be true;

The other option is to put the key field of the validation as UNIQUE and perform a try catch

try{
    $a = $db->query("INSERT INTO nombres (nombre, apellido, cedula) VALUES ('$nombre','$apellido','$cedula')");
    if(!$a){
        throw new Exception("Error ".$db->error,1);
    }
    var_dump($a);
}catch(Exception $e){
    var_dump($e->getMessage());
}

With this you have more control, since you decide that the function returns to validate if it is registered or not, in this case, we will return an error similar to this: string (49) "Error Duplicate entry '16401771 'for key' cedula '" indicating that the cedula data is already registered in the table and therefore the registration was not made.

I hope you serve

    
answered by 10.04.2018 в 17:25
2

You can do it in the following way

  • That the column in your BD is UNIQUE, you can achieve it in the following way

    ALTER TABLE ADD UNIQUE profiles (field_name);

  • 2.- Once you try to register, you can verify it in this way, from PHP

    if ($resultado->fetchColumn() > 0) {
        echo "El usuario ya existe"
    }else{
       echo "el usuario no existe";
    }
    
      

    It's always going to be better than your first verification or validation the   do from the database manager, with the UNIQUE attribute for   Avoid duplicate values

        
    answered by 10.04.2018 в 17:08
    1

    One option would be to make a query with the unique data of the user before inserting the data or, a validation at the database level adding a UNIQUE value:

    ALTER TABLE perfiles 
    ADD UNIQUE INDEX campo_unico_UNIQUE (campo_unico ASC)
    
        
    answered by 10.04.2018 в 17:01