Compare input data with records in MySQL

1

I have an input in HTML with AJAX which enters a 4-digit code with a button to send to a database in MySQL, until now everything is perfectly inserted, and registered in the database.

Now I have to compare that data that is entered in the input and when I click on the submit button, I compare that data in the input with all the records that the database has, that is, if compared to All the data you have in the database and find one like it send me a message saying that this data is already registered in the MySQL database.

To insert the data I use PHP. Next I put the code that I used to insert these values.

<?php

include('conectar.php');

if (isset($_POST['item']) && !empty($_POST['item'])) {

        $con=mysqli_connect($host,$user,$pw,$db);
        mysqli_query($con,("INSERT INTO TablaCodigos(item) VALUES ('$_POST[item]')"));
        echo "<script> alert('Datos Ingresados correctamente a la base de datos') </script>"; 

} else {

    echo "<script> alert('Error!! No se han podido ingresar datos a  la base de datos') </script>";
}


?>
    
asked by Staz 02.04.2018 в 18:10
source

2 answers

1

The first thing you should do is a SELECT to check if what you have in your input is already in the registry or not, I will attach a small code to you as you would, based on your exposed code, something may be missing because I did not debug it but the idea is this:

<?php

include('conectar.php');     

if (isset($_POST['item']) && !empty($_POST['item'])) {
    $con=mysqli_connect($host,$user,$pw,$db);

    //1 Hacer una consulta a la base de datos en tu tabla antes de registrar el registros       
    $item = $_POST['item']; 
    $dato =  mysqli_query($con, "SELECT item FROM TablaCodigos WHERE item = '".$item."'");

    // Verifica si encontro al menos un registro..Contar el numero de filas 
    $duplicado = mysqli_num_rows($dato);

    if($duplicado==0){
        //Quiere decir que no se encontró un item igual a la del input entonces inserta
        mysqli_query($con,("INSERT INTO TablaCodigos(item) VALUES ('$_POST[item]')"));
        echo "<script> alert('Datos Ingresados correctamente a la base de datos') </script>"; 
        }else{
        echo "<script> alert('Item duplicado') </script>";
        }   
} else {

echo "<script> alert('Error!! No se han podido ingresar datos a  la base de datos') </script>";
}
?>
    
answered by 02.04.2018 / 18:47
source
1

The way to achieve this is by consulting in the database if that value already exists. You could do it this way:

    if (isset($_POST['item']) && !empty($_POST['item'])) {

        $con=mysqli_connect($host,$user,$pw,$db);
        mysqli_query($con,("INSERT INTO TablaCodigos (item) VALUES('$_POST[item]') WHERE NOT EXISTS (SELECT * FROM TablaCodigos WHERE item='$_POST[item]')LIMIT 1 "));

        if($mysqli->affected_rows){
              echo "<script> alert('Datos Ingresados correctamente a la base de datos') </script>"; 
        }else{
              echo "<script> alert('Datos no ingresados...') </script>";

    } else {

        echo "<script> alert('Error!! No se han podido ingresar datos a  la base de datos') </script>";
    }

$mysqli->affected_rows; Returns the number of rows affected by the last query INSERT, UPDATE, REPLACE or DELETE.

The way in which you are using the example is to verify the amount of records inserted. But if it is not inserted it will not necessarily mean that the record was not inserted because it is a duplicate of a stored data.

    
answered by 02.04.2018 в 18:33