Appear an alert that can not be repeated

0

Hello good morning I have a file upload that is uploaded with a combobox which is called in the database image_text I would like that combobox would not be repeated the option put in the combobox this is my insert

<?php
  session_start();
  include "conectar.php";
  $link = conectar();

  if ($_SERVER['REQUEST_METHOD'] == "POST") {

    if($_POST['action'] == 'guardarRegistro') {

        $destino = '../files/';
        if(!file_exists($destino)) {
            mkdir($destino, 0777);
        }

        $fileName = $_FILES['archivo']['name'];
        $fileTemp = $_FILES['archivo']['tmp_name'];
        $fileType = substr($fileName, strrpos($fileName,".") + 1);
        //$nombre = date('dmYHis').$_POST['nombre'];
        $nombre = $_SESSION['id_inspeccion'].date('dmYHis')."_".".".$fileType;
        if(move_uploaded_file($fileTemp,$destino.$nombre)) {
            $sql = "INSERT INTO images(image,image_text,id_inspeccion) VALUES ('$nombre','".$_POST['imagenText']."','".$_SESSION['id_inspeccion']."')";

            if(mysqli_query($link,$sql)) {
              echo 1;
              die();
            } else {
              echo -2;
              echo $sql;
              die();
            }
        } else {
            echo -1;
            die();
        }

    }

  }

?>

and this is what I tried to do

<?php
  session_start();
  include "conectar.php";
  $link = conectar();

  if ($_SERVER['REQUEST_METHOD'] == "POST") {

    if($_POST['action'] == 'guardarRegistro') {

        $destino = '../files/';
        if(!file_exists($destino)) {
            mkdir($destino, 0777);
        }

        $fileName = $_FILES['archivo']['name'];
        $fileTemp = $_FILES['archivo']['tmp_name'];
        $fileType = substr($fileName, strrpos($fileName,".") + 1);
        //$nombre = date('dmYHis').$_POST['nombre'];
        $nombre = $_SESSION['id_inspeccion'].date('dmYHis')."_".".".$fileType;
        if(move_uploaded_file($fileTemp,$destino.$nombre)) {

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

    if($numeroRegistros == -1){
            $sql = "INSERT INTO images(image,image_text,id_inspeccion) VALUES ('$nombre','".$_POST['imagenText']."','".$_SESSION['id_inspeccion']."')";

            if(mysqli_query($link,$sql)) {
              echo 1;
              die();
            } else {
              echo -2;
              echo $sql;
              die();
            }
      }  } else {
            echo -1;
            die();
        }

    }

  }

?>
    
asked by JSACTM Music 04.10.2018 в 18:34
source

1 answer

0

The problem you have is in the condition that you have after the select that you do to verify if the record already exists, you should not place if($numeroRegistros == -1){ , you must put if($numeroRegistros == 0){ since you are indicating that if not there are matches or you did not find any previous records you can register or make insert .

    
answered by 04.10.2018 / 23:06
source