How do I prevent a "patient" from registering twice? PHP MySQL

2

I am creating a small "site" for a medical friend, who needs to keep track of patients, he needs to enter the data for each client as Full Name and Address, but I want to register a new patient, check if he is already is registered, taking into account Name and surname.

-DB Name "drfreddy"
-Tabla "patients"
-columns
 - "id
 - "name"
 - "last name"
 - "apellidom"
 - "address"
 - "telefono"

This is the complete code.

<?php /*session_start();

if (isset($_SESSION['usuario'])) {
    header('Location: index.php');
}*/

if ($_SERVER['REQUEST_METHOD'] == 'POST') {     
    $nombre = filter_var(($_POST['nombre']), FILTER_SANITIZE_STRING);
    $apellidop = filter_var(($_POST['apellidop']), FILTER_SANITIZE_STRING);
    $apellidom = filter_var(($_POST['apellidom']), FILTER_SANITIZE_STRING);
    $direccion = filter_var(($_POST['direccion']), FILTER_SANITIZE_STRING);
    $telefono = filter_var(($_POST['telefono']), FILTER_SANITIZE_STRING);



    $errores ='';

    if (empty($nombre) or empty($apellidop)  or empty($apellidom)  or empty($direccion)  or empty($telefono) ) 
    {
        $errores .= '<li>Por favor rellena todos los datos correctamente</li>';
    } 

    else    
    {
            try 
            {
            $conexion = new PDO('mysql:host=localhost;dbname=drfreddy', 'root', '');
            } 
            catch (PDOException $e) 
                {
                echo "Error: " . $e->getMessage();
                }
           //Aqui creo esta mi error...
            $statement = $conexion->prepare('SELECT nombre, apellidop, apellidop * FROM pacientes WHERE nombre = :nombre, apellidop = :apellidop, apellidom = :apellidom');

            $statement->execute(array(':nombre'  => $nombre, ':apellidop' => $apellidop, ':apellidom' => $apellidom));

            $resultado = $statement->fetch();

            if ($resultado != false) 
            {
            $errores .= '<li>El paciente ya existe en la base de datos</li>';
            }


    }  //Termina IF

    if ($errores == '') {
        $statement = $conexion->prepare('INSERT INTO pacientes (id, nombre, apellidop, apellidom, direccion, telefono) VALUES (null, :nombre, :apellidop, :apellidom, :direccion, :telefono)');

        $statement->execute(array(
            ':nombre' => $nombre,
            ':apellidop' => $apellidop,
            ':apellidom' => $apellidom,
            ':direccion' => $direccion,
            ':telefono' => $telefono
        ));


        header('Location: pacientes.php');
    }

}
require 'views/pacientes.view.php';

?>
    
asked by Rhodwulf 27.02.2017 в 05:04
source

3 answers

1

You can edit the MySQL table and add an Index that contains the name and surname field together. That index you identify as a unique value, that your table will not accept two records with those same fields.

In the SQL request you make an INSERT IGNORE, so when you insert a repeated record, it will not insert it and ignore the error.

You can check if the user has been inserted or if it was a repeated user by retrieving the mysqli_insert_id after doing the insert. In the case of being repeated that value will come empty.

    
answered by 27.02.2017 / 10:41
source
0

Try this:

if ($resultado != false){
    $errores .= '<li>El paciente ya existe en la base de datos</li>';
} else {
    $statement = $conexion->prepare('INSERT INTO pacientes (id, nombre, apellidop, apellidom, direccion, telefono) VALUES (null, :nombre, :apellidop, :apellidom, :direccion, :telefono)');

    $statement->execute(array(
        ':nombre' => $nombre,
        ':apellidop' => $apellidop,
        ':apellidom' => $apellidom,
        ':direccion' => $direccion,
        ':telefono' => $telefono
    ));


    header('Location: pacientes.php');
    /* Asegurándonos de que el código interior no será ejecutado cuando se realiza la redirección. */
    exit;
}

Note:
Remember that header () must be called before showing anything on screen, HTML tags, blank lines from a file or from PHP.

Note:
Remember to use exit after the header.

    
answered by 27.02.2017 в 23:44
0

There are several things to keep in mind:

  • you need some kind of unique index, use the name + last name as you can have problems if you have homonyms (it will happen for sure), I would recommend adding a single field, it can be the number of your country or until the email (although it may be that someone does not have it, which is rare).

  • Add a constraint of unique in the new column.

  • Handle errors when trying to create a repeated user, you have two options, handle the error that gives the database (by the unique) or make a query before to check that user does not exist.

  • Tip: Evaluate the possibility of using an ORM that does all this for you.

        
    answered by 07.03.2017 в 11:07