Do not repeat the data

-2

Hello good morning friends what happens to me is that I have an input text which I put a number and then I send it with a button and I give it as many times as I want and the number is repeated I would like to put a restriction that can not be repeated

This is my javascript

$('#btncreartipovehiculo').on('click', function(event){
    event.preventDefault();

    tipo_vehiculo = $('#tipo_vehiculo').val();
    placa = $('#placa').val();



if(tipo_vehiculo == '' || placa == ''){

      alert('Debe digitar los datos de la placa.');  

  } else {



      var datosPlaca = {
        tipo_vehiculo   : $ ('#tipo_vehiculo').val(),
        placa     : $('#placa').val()

      };

      $.post('php/agregarplaca.php', datosPlaca, function(data, textStatus, xhr) {
        console.log(data)
        if (data == 0){
          alert('No se pudo procesar el vehiculo.');
          return;
        }
        if (data == 1){
          alert('No ha guardado la inspeccion.');
          return;
        }
        if (data == 2){
          alert('No se pudo procesar. Error al insertar.');
          return;
        }


        if (data > 2){
          alert('Se ha guardado correctamente el registro: ' + data);
          $.post('php/mostrarplaca_actualiza.php', datosPlaca, function(data2, textStatus, xhr) {
            $('#datosplaca').html(data2);
          });
          $('#placa').val("");
          return;
        }
        alert('Error:' + data);
      });
    }


  });

and my insert

<?php
session_start();
include "conectar.php";
$tipo_vehiculo   = addslashes($_POST['tipo_vehiculo']);
$placa           = addslashes($_POST['placa']);


if (!empty($_POST)){ 
        if ($_SESSION['id_inspeccion'] == null or $_SESSION['id_inspeccion'] == ''){ 
            echo 1;
            die();
        }
        else {
            //echo 1;
            $sql =   "INSERT INTO vehiculo_inspeccion (tipo_vehiculo , placa , id_inspeccion) 
                            VALUES ( '" . $tipo_vehiculo . "',  '" . $placa . "',". $_SESSION['id_inspeccion'] .")";
            $con = Conectar();

            $con->query(utf8_decode($sql));

            if ( $con->affected_rows > 0){
                $nuevoId = $con->insert_id;
                echo $nuevoId;
            } 
            else{
                echo 2;
            }
        }
}
else{ 
    echo 0;
}










?>
    
asked by PlayTutoAlvarez 28.09.2018 в 14:50
source

2 answers

0

Before doing the insert you should only check if the combination PLATE - ID_INSPECTION exists, that is, you should make a selection or a query like this for example:

$sql = "SELECT * FROM vehiculo_inspeccion WHERE placa = '".$placa."' AND id_inspeccion = '".$_SESSION['id_inspeccion']."'";
$con = conectar();
$resultado = $con->query($sql);
$numeroRegistros = $resultado->num_rows;

  if($numeroRegistros == 0){

     AQUÍ HACES TU INSERT

  }ELSE{

     AQUÍ RETORNAS EL ERROR INDICANDO DE QUE YA EXISTE ESA PLACA PARA ESA INSPECCIÓN

  }
    
answered by 28.09.2018 / 17:07
source
3

The problem is that the license plate of the vehicle is not unique.

Run the following on your database engine.

ALTER TABLE vehiculo_inspeccion ADD CONSTRAINT PLACA_UK UNIQUE (placa);

So the license plate of the vehicles will not be repeated, as they will have a counterint unique key .

Here is more information

Greetings.

    
answered by 28.09.2018 в 16:20