How to validate php record that does not repeat? [duplicate]

0

Good morning: I am creating a file in php that connects to a localhost, what I need is to make it validate in the localhost that there are no duplicate records, the validation I have to do with the rfc (Mexican) since it is a unique value. I request your support since I'm just starting the race and I do not know php. Annex the code that I am using

<?php require_once('coneccion.php');  

    $nombre = $_POST["nombre"];
    $apellidopaterno = $_POST["apellidopaterno"];
    $apellidomaterno = $_POST["apellidomaterno"];
    $fechanacimiento= $_POST["fechanacimiento"];
    $rfc = $_POST["rfc"];

    $q = "INSERT INTO persona (id, nombre, apellidopaterno, apellidomaterno, $fechanacimiento, rfc) ";

    $q.= "VALUES(0,'".$nombre."','".$apellidopaterno."','".$apellidomaterno."','".$fechanacimiento."','".$rfc."')";


    $r = mysqli_query($conn,$q);
    if($r){
        $response["success"] = 1;
    } else{
        $response["success"] = 0;

    }
    echo json_encode($response);
?>  
    
asked by Luis Andrade H 19.09.2018 в 06:01
source

1 answer

0

I hope I have understood the question well.

You could do this to avoid duplicates:

    //Puedes poner la variable que quieras para verificar o varias
    $sql = "SELECT * FROM persona WHERE rfc = '".$rfc."'; 

    $r = mysqli_query($conn,$sql);

    //Vemos si devuelve algo con el count(contar)
    if (count($r) > 0) {

          $sql_insert = "INSERT........";
    }
    
answered by 19.09.2018 / 11:34
source